Is there a neat method could print the class name of a variable in Scala?
Sometimes I see val result = foo(a,b,c)
, since method foo
may return different types of object and if I could print class type of result, it will be great.
Is there a neat method could print the class name of a variable in Scala?
Sometimes I see val result = foo(a,b,c)
, since method foo
may return different types of object and if I could print class type of result, it will be great.
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: