我正在尝试编写以下内容:
val value: Int = 3
val tpe = typeOf(value) // This is pseudocode. What should
// actually go on this line?
q"""
type U = $tpe
val v: U = $value
"""
基本上,我需要捕获value
中Int
(tpe
)的类型并将其分配给U
。如何做到这一点?
答案 0 :(得分:2)
尝试
import scala.reflect.runtime.universe._
def getType[A: TypeTag](a: A): Type = typeOf[A]
val value: Int = 3
val tpe = getType(value) // Int
相关问题:How does one obtain the type of the value that an AST represents?