I've been playing around with the new API for the compiler and repl in 2.11 and have hit something weird. Here's my repl output:
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
As you can see, typeOfTerm("x")
returns ()Int
, but typeOfExpression("x")
returns Int
. I think it's the case that the type ()Int
represents a variable of type Int
, but I can't be sure. If someone could confirm that or correct my confusion and maybe direct me to any documentation that talks about this, I'd appreciate it. I looked through the reflection docs that I could find, but didn't have any luck.