print type of variable in Scala

Use Object.getClass method.

See Scala Cookbook


Quick and dirty trick I often use

val result: Nothing = foo(a,b,c)

The compiler will raise an error, providing you information about the actual return type it found.

Example from the REPL

scala> val foo: Nothing = 42
<console>:1: error: type mismatch;
found   : Int(42) // <---- this is what you care about
required: Nothing
      val foo: Nothing = 42

If you use an IDE, you may use more "sophisticated" options:

  • Scala IDE: http://scala-ide.org/docs/current-user-doc/features/typingviewing/show-type-of-selection.html
  • IntelliJ: How do I view the type of a scala expression in IntelliJ

Tags:

Scala