I’m trying to write the following:
import scala.reflect.runtime.universe._
val value: Tree = /* some AST */
val tpe = typeOf(value) // This should be the result type of the AST.
// This is pseudocode. What should
// actually go on this line?
q"""
type U = $tpe
val v: U = $value
"""
I need to capture the type of the value represented by the AST value
in tpe
and assign it to U
. How does one do this?
Edit: Giving a type annotation for value
and matching on it via quasiquotes isn't an option here. The use case is a Shapeless extensible record, which has complicated types such as String with labelled.KeyTag[1, String] :: Long with labelled.KeyTag[three, Long] :: HNil
for something like val ls = (1 ->> "two") :: ("three" ->> 4L) :: HNil
. Also, the value
AST is programmatically generated and not a literal.