I want to get the type of a variable at runtime

アレックス picture アレックス · Oct 15, 2013 · Viewed 138k times · Source

I want to get the type of a variable at runtime. How do I do this?

Answer

Daniel C. Sobral picture Daniel C. Sobral · Oct 15, 2013

So, strictly speaking, the "type of a variable" is always present, and can be passed around as a type parameter. For example:

val x = 5
def f[T](v: T) = v
f(x) // T is Int, the type of x

But depending on what you want to do