scala quasiquotes string variable lifting in multiple steps

314 views Asked by At

this is what i want:

scala> var x:Int = 10
x: Int = 10

scala> var y:Int = 20
y: Int = 20

scala> val ret = q"return $x>$y"
ret: universe.Return = return 10.$greater(20)

scala> val result1 = toolbox.compile(ret)()
result1: Any = false

But the problem is that I will be getting the expression $x>$y in a string var, like

scala> m
res20: String = $x>$y

And then I want to perform the operation as,

var ret = q"return $m"

but this return:

scala> var ret = q"return $m"
ret: universe.Return = return "$x>$y"

which is not serving the purpose. How can I get the value of x and y in the last step instead $x and $y.

1

There are 1 answers

1
quaspas On

You can mix quasiquotes and ToolBox.parse to achieve results mostly like what you are looking for:

import scala.tools.reflect.ToolBox
import scala.reflect.runtime.universe._

val toolbox = scala.reflect.runtime.currentMirror.mkToolBox()

val s = "10>20"
val ret = q"return ${toolbox.parse(s)}"
// reflect.runtime.universe.Return = return 10.$greater(20)
toolbox.eval(ret)
// Any = false

Making use of your values as variables:

import scala.tools.reflect.ToolBox
import scala.reflect.runtime.universe._
val toolbox = scala.reflect.runtime.currentMirror.mkToolBox()
val x = 10
val y = 20
val m = "x>y"
val ret = q"return ${toolbox.parse(m)}"
// reflect.runtime.universe.Return = return x.$greater(y)

If you evaluate q"return ${toolbox.parse(m)}" you will see the error because x and y are not in scope.

scala> toolbox.eval(q"return ${toolbox.parse(m)}")
scala.tools.reflect.ToolBoxError: reflective compilation has failed:

object > is not a member of package x ...

To fix that:

import scala.tools.reflect.ToolBox
import scala.reflect.runtime.universe._
val toolbox = scala.reflect.runtime.currentMirror.mkToolBox()
val q1 = q"val x = 10; val y = 20; "
val m = "x>y"
val ret = q"""
     |   ..$q1
     |   return ${toolbox.parse(m)}
     | """
toolbox.eval(ret)
// Any = false