Skip to content

Commit

Permalink
feat: Add compile-time Last
Browse files Browse the repository at this point in the history
  • Loading branch information
Iltotore committed Sep 20, 2024
1 parent 9c54fa3 commit f6684ad
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions main/src/io/github/iltotore/iron/constraint/collection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ object collection:
expr.toExprList match
case Some(list) =>
list.headOption match
case Some(head) => applyConstraint(list.head, constraintExpr)
case Some(head) => applyConstraint(head, constraintExpr)
case None => Expr(false)

case None => '{ $expr.exists(c => ${ applyConstraint('c, constraintExpr) }) }
Expand All @@ -400,7 +400,7 @@ object collection:

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

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

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

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

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

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.lastOption match
case Some(last) => applyConstraint(last, constraintExpr)
case None => Expr(false)

case None => '{ $expr.lastOption.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 f6684ad

Please sign in to comment.