Skip to content

Commit

Permalink
Add quoted.Liftable[Map[T, U]] to the stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed Jul 25, 2019
1 parent 4cfbff9 commit 08d9b6b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
5 changes: 5 additions & 0 deletions library/src/scala/quoted/Liftable.scala
Expand Up @@ -71,6 +71,11 @@ object Liftable {
'{ Set(${set.toSeq.toExpr}: _*) }
}

given [T: Type: Liftable, U: Type: Liftable] as Liftable[Map[T, U]] = new Liftable[Map[T, U]] {
def toExpr(map: Map[T, U]): given QuoteContext => Expr[Map[T, U]] =
'{ Map(${map.toSeq.toExpr}: _*) }
}

given [T: Type: Liftable] as Liftable[Option[T]] = new Liftable[Option[T]] {
def toExpr(x: Option[T]): given QuoteContext => Expr[Option[T]] = x match {
case Some(x) => '{ Some[T](${x.toExpr}) }
Expand Down
3 changes: 3 additions & 0 deletions tests/run-with-compiler/quote-lib.scala
Expand Up @@ -56,6 +56,9 @@ object Test {
val set: Set[Int] = Set(1, 2, 3)
val liftedSet: Expr[Set[Int]] = set

val map: Map[Int, Char] = Map(1 -> 'a', 2 -> 'b', 3 -> 'c')
val liftedMap: Expr[Map[Int, Char]] = map

liftedList.foldLeft[Int](0)('{ (acc: Int, x: Int) => acc + x }).show
liftedList.foreach('{ (x: Int) => println(x) }).show

Expand Down

0 comments on commit 08d9b6b

Please sign in to comment.