Here are some snippets from my Scala prompt. I import the reflection API and try reifying some expressions, as described in the docs here.
scala> import scala.reflect.runtime.{universe => ru}
scala> val str = "Duck I says."
scala> ru.showRaw(ru.reify(println(2)))
res40: String = Expr(Apply(Select(Select(This(newTypeName("scala")),
newTermName("Predef")), newTermName("println")), List(Literal(Constant(2)))))
scala> ru.showRaw(ru.reify(str.length))
res41: String = Expr(Apply(Select(Select(Select(Select(Select(Ident($line4),
newTermName("$read")), newTermName("$iw")), newTermName("$iw")),
newTermName("str")), newTermName("length")), List()))
I did not expect to see these symbols $line4
, $read
, and $iw
in the second one. What are they and why are they there?
val str ...
in REPL is actually not a locale variable, but a property of some object. See this answer.
Wrap variable definition and reify
call with code block like this:
{
val str = ...
showRaw{reify {...}}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With