Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Scala中经过具体化、类型检查的“树”中提取类型_Scala_Types - Fatal编程技术网

从Scala中经过具体化、类型检查的“树”中提取类型

从Scala中经过具体化、类型检查的“树”中提取类型,scala,types,Scala,Types,鉴于以下REPL会话: scala> import scala.reflect.runtime.universe._ import scala.reflect.runtime.universe._ scala> import scala.tools.reflect.ToolBox import scala.tools.reflect.ToolBox scala> import scala.reflect.runtime.{currentMirror => cm} im

鉴于以下REPL会话:

scala> import scala.reflect.runtime.universe._
import scala.reflect.runtime.universe._

scala> import scala.tools.reflect.ToolBox
import scala.tools.reflect.ToolBox

scala> import scala.reflect.runtime.{currentMirror => cm}
import scala.reflect.runtime.{currentMirror=>cm}

scala> val r = reify { val a = 234.45 }
r: reflect.runtime.universe.Expr[Unit] =
Expr[Unit]({
  val a = 234.45;
  ()
})

scala> val c = cm.mkToolBox().typecheck(r.tree)
warning: there was one feature warning; re-run with -feature for details
c: qual$2.u.Tree forSome { val qual$2: scala.tools.reflect.ToolBox[reflect.runtime.universe.type] @scala.reflect.internal.annotations.uncheckedBounds } =
{
  val a: Double = 234.45;
  ()
}
从类型检查树
c
中拉出变量类型
a
的推荐方法是什么?

您可以使用,但我不确定这是最简单的方法:

scala> val c = cm.mkToolBox().typecheck(r.tree)
warning: there was one feature warning; re-run with -feature for details
c: qual$2.u.Tree forSome { val qual$2:     scala.tools.reflect.ToolBox[reflect.runtime.universe.type]     @scala.reflect.internal.annotations.uncheckedBounds } =
{
  val a: Double = 234.45;
  ()
}

scala> val q"{val $name: $tpt = $rhs; ()}" = c
name: reflect.runtime.universe.TermName = a
tpt: reflect.runtime.universe.Tree = Double
rhs: reflect.runtime.universe.Tree = 234.45

scala> :t tpt.tpe
reflect.runtime.universe.Type

scala> println(tpt.tpe)
Double

杰出的这就好像类语言是
具体化的
不适用的
。更像是,具体化是什么
val q“val$name:$tpt=$rhs”=currentmirr.mkToolBox().typecheck(q“val a=234.45”)