HOWTO · Scala
How to Find Type of Variable in Scala
This article talks about how to find type of a variable in Scala.
On this page
Scala has no single typeof operation that answers every type question. A variable’s declared or inferred type is a compile-time property; at runtime, you can inspect an object’s class, branch on known types, check a value against a type, or request generic type metadata. Choose the technique based on the information your code needs.
Choose the right meaning of runtime type
Use pattern matching when you know the cases your program handles. Use getClass to inspect an object’s runtime class, and getSimpleName when a shorter display name is useful. Use isInstanceOf for a Boolean check against one specified type. For generic type information, Scala also has Manifest and TypeTag; these are metadata mechanisms, not universal replacements for the other operations.
Use pattern matching for known types
Pattern matching checks the value against the cases you write. This standalone example labels a few common values and returns a fallback for other values. The launcher disables Scala’s compile daemon to avoid hostname lookup when running the script:
scala -nocompdaemon -e '
def label(value: Any): String = value match {
case _: String => "String"
case _: Int => "Int"
case _: Double => "Double"
case _ => "Other"
}
List[Any]("hello", 42, 3.14).foreach(value => println(label(value)))
'
String
Int
Double
This approach is useful when each known case needs different behavior. It does not discover every possible type automatically. For a sealed custom hierarchy, match its concrete case classes and include a fallback when the hierarchy or input may extend beyond the cases handled here.
Inspect an object’s runtime class with getClass
Call value.getClass to obtain the runtime Class for an object. For example, val runtimeClass = value.getClass lets code inspect that class or compare it with another class. This describes the object’s runtime class; it does not report the variable’s declared type, and it is not a complete description of generic type arguments.
For a shorter display label, use value.getClass.getSimpleName. Treat that string as presentation text: class names can vary with the implementation, and a simple name is not a stable type-checking API. A custom instance can be inspected the same way as a built-in object.
Recover generic type information with Manifest and TypeTag
Generic type parameters are not always available from an object at runtime. Scala’s Manifest and TypeTag provide type metadata that can be requested in generic code, subject to the metadata available for the type and the Scala environment in use. For example, Scala code can declare a method parameter such as (implicit m: Manifest[T]) or use a TypeTag[T] context bound and inspect the metadata supplied for T.
These mechanisms can help when a generic method needs information about its type parameter. They do not guarantee complete information for every value, nor should their string representation be treated as a stable serialization format. If the task is only to branch on a few known cases or check one type, pattern matching or isInstanceOf is more direct.
Check a value with isInstanceOf
Use value.isInstanceOf[SomeType] when the result you need is simply true or false. This works for a specified custom type as well as for a type such as String. The expression answers whether the value matches that type check; it does not return a type description or reveal a collection’s element type.
Prefer pattern matching if each type needs its own branch, since it combines the check and the handling in one expression. Avoid using a type check to infer generic arguments that are not available at runtime.
Choose the method that fits your task
- To handle a known set of alternatives, use pattern matching and provide a fallback.
- To inspect or display an object’s runtime class, use
getClass; usegetSimpleNameonly for a concise label. - To ask a yes-or-no question about one specified type, use
isInstanceOf. - To request generic type metadata in generic code, consider
ManifestorTypeTag, while accounting for the metadata available in your Scala environment.
None of these is a universal typeof: the declared type belongs to compile-time checking, while runtime operations expose only the particular class, check, or metadata they are designed to provide.