1

私は 2.11 のコンパイラと repl の新しい API をいじっていて、奇妙なことにぶつかりました。これが私のrepl出力です:

Welcome to Scala version 2.11.0-20140415-163722-cac6383e66 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_51).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import scala.tools.nsc.interpreter.IMain
import scala.tools.nsc.interpreter.IMain

scala> import scala.tools.nsc.Settings
import scala.tools.nsc.Settings

scala> val eng = new IMain(new IMain.Factory(), new Settings())
eng: scala.tools.nsc.interpreter.IMain = scala.tools.nsc.interpreter.IMain@649b982e

scala> eng.interpret("val x: Int = 2")
x: Int = 2
res0: scala.tools.nsc.interpreter.IR.Result = Success

scala> eng.valueOfTerm("x")
res2: Option[Any] = Some(2)

scala> eng.typeOfTerm("x")
res3: eng.global.Type = ()Int

scala> eng.typeOfExpression("x")
res4: eng.global.Type = Int

scala> eng.typeOfExpression("x") =:= eng.global.definitions.IntTpe
res6: Boolean = true

scala> eng.typeOfTerm("x") =:= eng.global.definitions.IntTpe
res7: Boolean = false

ご覧のとおり、 をtypeOfTerm("x")返します()Intが、 をtypeOfExpression("x")返しますInt()Inttypeが type の変数を表す場合だと思いますIntが、よくわかりません。誰かがそれを確認したり、私の混乱を修正したり、これについて話しているドキュメントに私を案内してくれたりしたら、私はそれを感謝します. 見つけたリフレクション ドキュメントを調べましたが、運がありませんでした。

4

1 に答える 1