如何使用准引数获取值的类型?

时间:2019-04-19 23:47:35

标签: scala scala-macros scala-quasiquotes scala-meta

我正在尝试编写以下内容:

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
"""

基本上,我需要捕获valueInttpe)的类型并将其分配给U。如何做到这一点?

1 个答案:

答案 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?