In REPL, your val x
is not a local definition. It is wrapped in a class or object, which leads to differing behaviors.
Your object X { val x = 2 }
has a member x
that is a parameterless method type, as if you had def x = 2
.
scala> val x: Int = 2
x: Int = 2
scala> $intp.typeOfTerm("x")
res0: $intp.global.Type = ()Int
scala> import $intp.global._
import $intp.global._
scala> $intp.replScope.lookup(TermName("x"))
res2: $intp.global.Symbol = value x
scala> res2.tpe
res3: $intp.global.Type = ()Int
scala> :power
** Power User mode enabled - BEEP WHIR GYVE **
** :phase has been set to 'typer'. **
** scala.tools.nsc._ has been imported **
** global._, definitions._ also imported **
** Try :help, :vals, power.<tab> **
scala> intp.replScope.lookup(TermName("x"))
res6: $r.intp.global.Symbol = value x
This is how we're used to seeing it:
scala> .tpe
res7: $r.intp.global.Type = => Int
scala> :phase
Active phase is 'Typer'. (To clear, :phase clear)
scala> :phase clear
Cleared active phase.
scala> res6.tpe
res8: $r.intp.global.Type = ()Int