User1291
User1291

Reputation: 8182

scala - insert value into quasiquote

Unfortunately, the most intuitive way,

val world = "Earth"
val tree = q"""println("Hello $world")"""

results in

Error:(16, 36) Don't know how to unquote here
val tree = q"""println("Hello $world")"""
                      ^

because $ within quasiquotes expects a tree.

val world = "Earth"
val tree = q"""println(${c.literal(s"Hello $world")})"""

works, but is very ugly AND I get an Intellij warning that the c.literal is deprecated and I should use quasiquotes, instead.

So ... how do I do this?

UPDATE

In response to flavian's comment:

import scala.language.experimental.macros
import scala.reflect.macros._

object TestMacros {

  def doTest() = macro impl

  def impl(c: blackbox.Context)(): c.Expr[Unit] = {
    import c.universe._ //access to AST classes
    /*
    val world = "Earth"
    val tree = q"""println(${c.literal(s"Hello $world")})"""
    */

    val world = TermName("Earth")
    val tree = q"""println("Hello $world")"""

    tree match {
      case q"""println("Hello Earth")""" => println("succeeded")
      case _ => c.abort(c.enclosingPosition, s"huh? was: $tree")
    }

    c.Expr(tree) //wrap tree and tag with its type
  }
}

gives

Error:(18, 40) Don't know how to unquote here
    val tree = q"""println("Hello $world")"""
                                   ^

Upvotes: 3

Views: 1349

Answers (2)

Darren Bishop
Darren Bishop

Reputation: 2585

I just started learning Macro-Fu.

For those also exploring Scala 2 Macros / Scalameta Quasiquotes, seems to me the simplest approach is the following (using SBT 1.5.5; inline explanations):

scala> import scala.language.experimental.macros
     | import scala.reflect.macros.blackbox
     |
     | object UnquoteString {
     |
     |   def helloWorld(): Unit = macro Impl.helloWorld
     |
     |   object Impl {
     |     def helloWorld(c: blackbox.Context)(): c.Expr[Unit] = {
     |       import c.universe._
     |
     |       val world = "Earth" // or any other value here...
     |
     |       val msg = s"Hello $world" // build the entire string with it here...
     |
     |       implicitly[Liftable[String]] // as we can lift 'whole' string values with this Liftable[_]...
     |
     |       val tree = q"""println($msg)""" // quasi-unquote the entire string here...
     |
     |       c.Expr[Unit](Block(Nil, tree))
     |     }
     |   }
     | }
import scala.language.experimental.macros
import scala.reflect.macros.blackbox
object UnquoteString

scala> UnquoteString.helloWorld()
Hello Earth

scala>

Also the following change would also work

      val tree = q"""println("Hello, " + $world)""" // quasi-unquote the string to append here...

Upvotes: 0

flavian
flavian

Reputation: 28511

You need a TermName or something that's a compiler primitive.

The real problem is that you are mixing interpolators, without realising. The interpolator in hello world is really a string interpolator, not a quasiquote one which is good at unquoting trees as you suggest.

This is one way to go about it:

import c.universe._

val world = TermName("Earth")
val tree = q"""println("Hello" + ${world.decodedName.toString})"""

Upvotes: 7

Related Questions