我想解析一段包含在 a 中的 scala 代码String
并获得该reflect.runtime.universe.Type
表达式 ( String => Type
) 的结果。我试过了:
scala> import scala.tools.nsc.interpreter.IMain
import scala.tools.nsc.interpreter.IMain
scala> new IMain().exprTyper.parse("scala.Option")
warning: there were 1 feature warning(s); re-run with -feature for details
res1: Option[List[_2.repl.global.Tree]] forSome { val _2: scala.tools.nsc.interpreter.IMain#exprTyper.type } = Some(List(scala.Option))
scala> .get.head
warning: there were 1 feature warning(s); re-run with -feature for details
res2: _2.repl.global.Tree forSome { val _2: scala.tools.nsc.interpreter.IMain#exprTyper.type } = scala.Option
scala> .tpe
res3: _2.type#repl.global.Type = null
scala> new IMain().runtimeTypeOfTerm("scala.Option")
warning: there were 1 feature warning(s); re-run with -feature for details
res5: _74.global.Type forSome { val _74: scala.tools.nsc.interpreter.IMain } = <notype>
scala> new IMain().typeOfTerm("scala.Option")
warning: there were 1 feature warning(s); re-run with -feature for details
res6: _75.global.Type forSome { val _75: scala.tools.nsc.interpreter.IMain } = <notype>
有可能吗,如果有,你是怎么做的?谢谢。
UPD:我已经表达了实际意图以及我现在在这个问题中的位置:Getting type information inside scala repl via IMain