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

How does one use quasiquotes to obtain the type of a value?

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

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

本质上,我需要捕获tpevalueInt)的类型,并赋值给U。如何做到这一点?

尝试

import scala.reflect.runtime.universe._
def getType[A: TypeTag](a: A): Type = typeOf[A]

val value: Int = 3
val tpe = getType(value) // Int

相关问题: