Skip to content

Commit

Permalink
Use reuse toExprOfSeq and toExprOfList
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed Jul 26, 2019
1 parent 5796226 commit 36186f0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
9 changes: 8 additions & 1 deletion library/src-non-bootstrapped/scala/quoted/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ package object quoted {
throw new Exception("Non bootsrapped library")

implicit object ExprOps {
def (x: T) toExpr[T: Liftable] given QuoteContext: Expr[T] = the[Liftable[T]].toExpr(x)
def (x: T) toExpr[T: Liftable] given QuoteContext: Expr[T] =
throw new Exception("Non bootsrapped library")

def (seq: Seq[Expr[T]]) toExprOfSeq[T] given (tp: Type[T], qctx: QuoteContext): Expr[Seq[T]] =
throw new Exception("Non bootsrapped library")

def (list: List[Expr[T]]) toExprOfList[T] given Type[T], QuoteContext: Expr[List[T]] =
throw new Exception("Non bootsrapped library")
}
}
12 changes: 4 additions & 8 deletions library/src/scala/quoted/Liftable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,13 @@ object Liftable {
}

given [T: Type: Liftable] as Liftable[Seq[T]] = new Liftable[Seq[T]] {
def toExpr(seq: Seq[T]): given QuoteContext => Expr[Seq[T]] = given qctx => {
import qctx.tasty._
Repeated(seq.map(x => the[Liftable[T]].toExpr(x).unseal).toList, the[quoted.Type[T]].unseal).seal.asInstanceOf[Expr[Seq[T]]]
}
def toExpr(xs: Seq[T]): given QuoteContext => Expr[Seq[T]] =
xs.map(the[Liftable[T]].toExpr).toExprOfSeq
}

given [T: Type: Liftable] as Liftable[List[T]] = new Liftable[List[T]] {
def toExpr(x: List[T]): given QuoteContext => Expr[List[T]] = x match {
case x :: xs => '{ (${xs.toExpr}).::[T](${x.toExpr}) }
case Nil => '{ Nil: List[T] }
}
def toExpr(xs: List[T]): given QuoteContext => Expr[List[T]] =
xs.map(the[Liftable[T]].toExpr).toExprOfList
}

given [T: Type: Liftable] as Liftable[Set[T]] = new Liftable[Set[T]] {
Expand Down

0 comments on commit 36186f0

Please sign in to comment.