Skip to content

Commit

Permalink
feat: Add compile-time Exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Iltotore committed Sep 20, 2024
1 parent 5a2667e commit bbe7ce6
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion main/src/io/github/iltotore/iron/constraint/collection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ object collection:

class ExistsIterable[A, I <: Iterable[A], C, Impl <: Constraint[A, C]](using Impl) extends Constraint[I, Exists[C]]:

override inline def test(inline value: I): Boolean = value.exists(summonInline[Impl].test(_))
override inline def test(inline value: I): Boolean = ${ checkIterable('value, '{ summonInline[Impl] }) }

override inline def message: String = "At least one: (" + summonInline[Impl].message + ")"

Expand All @@ -327,6 +327,18 @@ object collection:

inline given existsString[C, Impl <: Constraint[Char, C]](using inline impl: Impl): ExistsString[C, Impl] = new ExistsString

private def checkIterable[A : Type, I <: Iterable[A] : Type, C, Impl <: Constraint[A, C]](expr: Expr[I], constraintExpr: Expr[Impl])(using Quotes): Expr[Boolean] =
val rflUtil = reflectUtil
import rflUtil.*

expr.toExprList match
case Some(list) =>
list
.map(applyConstraint(_, constraintExpr))
.foldLeft(Expr(false))((e, t) => '{ $e || $t })

case None => '{ $expr.exists(c => ${ applyConstraint('c, constraintExpr) }) }

private def checkString[C, Impl <: Constraint[Char, C]](expr: Expr[String], constraintExpr: Expr[Impl])(using Quotes): Expr[Boolean] =
val rflUtil = reflectUtil
import rflUtil.*
Expand Down

0 comments on commit bbe7ce6

Please sign in to comment.