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

风流意气都作罢 提交于 2019-12-11 14:08:59

问题


I’m trying to write the following:

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

Essentially, I need to capture the type of value (Int) in tpe and assign it to U. How does one do this?


回答1:


Try

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

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

Connected question: How does one obtain the type of the value that an AST represents?



来源:https://stackoverflow.com/questions/55768992/how-does-one-use-quasiquotes-to-obtain-the-type-of-a-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!