[英]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
"""
本质上,我需要捕获tpe
的value
类型( Int
)并将其分配给U
如何做到这一点?
尝试
import scala.reflect.runtime.universe._
def getType[A: TypeTag](a: A): Type = typeOf[A]
val value: Int = 3
val tpe = getType(value) // Int
相关问题: 如何获得AST表示的值的类型?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.