@@ -507,7 +507,7 @@ The compiler takes an environment that maps variable names to Scala `Expr`s.
507507
508508 def compile(e: Exp, env: Map[String, Expr[Int]]): Expr[Int] = e match {
509509 case Num(n) =>
510- n
510+ n.toExpr
511511 case Plus(e1, e2) =>
512512 ’(~compile(e1, env) + ~compile(e2, env))
513513 case Var(x) =>
@@ -520,24 +520,25 @@ Running `compile(letExp, Map())` would yield the following Scala code:
520520
521521 ’{ val y = 3; (2 + y) + 4 }
522522
523- The body of the first clause, ` case Num(n) => n ` , looks suspicious. ` n `
524- is declared as an ` Int ` , yet the result of ` compile ` is declared to be
525- ` Expr[Int] ` . Shouldn’t ` n ` be quoted? In fact this would not
523+ The body of the first clause, ` case Num(n) => n.toExpr ` , looks suspicious. ` n `
524+ is declared as an ` Int ` , yet it is conveted to an ` Expr[Int] ` with ` toExpr ` .
525+ Shouldn’t ` n ` be quoted? In fact this would not
526526work since replacing ` n ` by ` ’n ` in the clause would not be phase
527527correct.
528528
529- What happens instead "under the hood" is an implicit conversion : ` n `
530- is expanded to ` scala.quoted.Expr.toExpr (n) ` . The ` toExpr ` conversion
531- is defined in the companion object of class ` Expr ` as follows:
529+ What happens instead "under the hood" is an extension method ` toExpr ` is added : ` n.toExpr `
530+ is expanded to ` new scala.quoted.LiftExprOps (n).toExpr ` . The ` toExpr ` extension
531+ is defined in the companion object of class ` Liftable ` as follows:
532532
533- object Expr {
534- implicit def toExpr[T](x: T)(implicit ev: Liftable[T]): Expr[T] =
535- ev.toExpr(x)
533+ package object quoted {
534+ implicit class LiftExprOps[T](val x: T) extends AnyVal {
535+ def toExpr(implicit ev: Liftable[T]): Expr[T] = ev.toExpr(x)
536+ }
536537 }
537538
538- The conversion says that values of types implementing the ` Liftable `
539- type class can be converted ("lifted") automatically to ` Expr `
540- values. Dotty comes with instance definitions of ` Liftable ` for
539+ The extension says that values of types implementing the ` Liftable ` type class can be
540+ converted ("lifted") to ` Expr ` values using ` toExpr ` when ` scala.quoted._ ` is imported.
541+ Dotty comes with instance definitions of ` Liftable ` for
541542several types including ` Boolean ` , ` String ` , and all primitive number
542543types. For example, ` Int ` values can be converted to ` Expr[Int] `
543544values by wrapping the value in a ` Literal ` tree node. This makes use
@@ -570,7 +571,7 @@ a `List` is liftable if its element type is:
570571
571572 implicit def ListIsLiftable[T: Liftable]: Liftable[List[T]] = new {
572573 def toExpr(xs: List[T]): Expr[List[T]] = xs match {
573- case x :: xs1 => ’(~implicitly[Liftable[T]] .toExpr(x) :: ~toExpr(xs1))
574+ case x :: xs1 => ’(~x .toExpr :: ~toExpr(xs1))
574575 case Nil => ’(Nil: List[T])
575576 }
576577 }
0 commit comments