Question

The Scala REPL shows the inferred type of an expression. Is there a way to know the inferred type in a normal Scala program ?

For example,

val x = {
//some Scala expressions
}

Now I want to know the actual type of x.

Was it helpful?

Solution

Perhaps TypeTag is what you are looking for?

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

scala> def typeOf[T](x:T)( implicit tag: TypeTag[T] ) = tag
typeOf: [T](x: T)(implicit tag: reflect.runtime.universe.TypeTag[T])reflect.runtime.universe.TypeTag[T]

scala> class Foo( a:Int )
defined class Foo

scala> trait Bar
defined trait Bar

scala> val x = new Foo(3) with Bar
x: Foo with Bar = $anon$1@62fb343d

scala> val t = typeOf(x)
t: reflect.runtime.universe.TypeTag[Foo with Bar] = TypeTag[Foo with Bar]

scala> t.tpe
res20: reflect.runtime.universe.Type = Foo with Bar

scala> t.tpe.toString
res21: String = Foo with Bar

And just to demonstrate that it yields the static type of the expression rather than the dynamic type of the object:

scala> val l = List(1,2,3)
l: List[Int] = List(1, 2, 3)

scala> val s:Seq[Int] = l
s: Seq[Int] = List(1, 2, 3)

scala> typeOf(s)
res22: reflect.runtime.universe.TypeTag[Seq[Int]] = TypeTag[scala.Seq[Int]]

OTHER TIPS

The type of the expression is known statically at compile time.

To access it at runtime, you could use a TypeTag as in the other answer, or a trivial macro:

scala> import scala.language.experimental.macros
import scala.language.experimental.macros

scala> import reflect.macros.blackbox.Context
import reflect.macros.blackbox.Context

scala> def impl(c: Context)(x: c.Expr[Any]): c.Expr[String] = { import c.universe._
     | c.Expr[String](Literal(Constant(c.typecheck(x.tree.duplicate).tpe.toString))) }
impl: (c: scala.reflect.macros.blackbox.Context)(x: c.Expr[Any])c.Expr[String]

scala> def f(x: =>Any) = macro impl
warning: there were 1 deprecation warning(s); re-run with -deprecation for details
defined term macro f: (x: => Any)String

scala> trait A; trait B extends A; trait C extends A
defined trait A
defined trait B
defined trait C

scala> f(List(new B{}, new C{}))
res2: String = List[A]

The REPL also just reports the type assigned to an expression tree by the compiler.

when using Metals with Scala, you can hover over the x variable in your expression and the type is then instantly displayed (having compiled your program), or your enable Metals: Toggle showing inferred type then the type is directly shown in grey color beside the variable name while hovering over with the mouse.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top