Skip to content

Commit

Permalink
feat: Add expr list support
Browse files Browse the repository at this point in the history
  • Loading branch information
Iltotore committed Sep 20, 2024
1 parent a55d33c commit d7ce3ff
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
16 changes: 16 additions & 0 deletions main/src/io/github/iltotore/iron/compileTime.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import scala.compiletime.constValue
import scala.compiletime.ops.*
import scala.compiletime.ops.any.ToString
import scala.quoted.*
import scala.annotation.targetName

/**
* Methods and types to ease compile-time operations.
Expand Down Expand Up @@ -199,3 +200,18 @@ object compileTime:
import quotes.reflect.*

Apply(Select.unique(constraintExpr.asTerm, "test"), List(expr.asTerm)).asExprOf[Boolean]

extension [T : Type](expr: Expr[Iterable[T]])

def toExprList(using Quotes): Option[List[Expr[T]]] = expr match
case '{ scala.List[T](${Varargs(elems)}*) } => Some(elems.toList)
case '{ scala.List.empty[T] } => Some(Nil)
case '{ Nil } => Some(Nil)
case '{ scala.collection.immutable.List[T](${Varargs(elems)}*) } => Some(elems.toList)
case '{ scala.collection.immutable.List.empty[T] } => Some(Nil)
case '{ Set[T](${Varargs(elems)}*) } => Some(elems.toList)
case '{ Set.empty[T] } => Some(Nil)
case '{ scala.collection.immutable.List[T](${Varargs(elems)}*) } => Some(elems.toList)
case '{ scala.collection.immutable.List.empty[T] } => Some(Nil)
case _ => None

14 changes: 14 additions & 0 deletions main/src/io/github/iltotore/iron/constraint/collection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,20 @@ object collection:

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

private def checkIterable[I <: Iterable[?], C, Impl <: Constraint[Char, C]](expr: Expr[I], constraintExpr: Expr[Impl])(using Quotes): Expr[Boolean] =
???
/*val rflUtil = reflectUtil
import rflUtil.*
expr.decode match
case Right(value) =>
value
.map(Expr.apply)
.map(applyConstraint(_, constraintExpr))
.foldLeft(Expr(true))((e, t) => '{ $e && $t })
case _ => '{ $expr.forallOptimized(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
3 changes: 1 addition & 2 deletions main/src/io/github/iltotore/iron/macros/ReflectUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -390,5 +390,4 @@ class ReflectUtil[Q <: Quotes & Singleton](using val _quotes: Q):
term match
case Apply(TypeApply(Select(Ident("Set"), "apply"), _), List(values)) =>
decodeTerm(values, definitions).as[List[?]].map(_.toSet)
case _ => Left(DecodingFailure.Unknown)

case _ => Left(DecodingFailure.Unknown)
2 changes: 1 addition & 1 deletion main/src/io/github/iltotore/iron/macros/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ def isIronTypeImpl[T: Type, C: Type](using Quotes): Expr[Boolean] =
case _: ImplicitSearchSuccess => Expr(true)
case _: ImplicitSearchFailure => Expr(false)

case _ => Expr(false)
case _ => Expr(false)

0 comments on commit d7ce3ff

Please sign in to comment.