WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Scala · Advanced · question 46 of 100

Explain the concept of type erasure in Scala and how to work around it using TypeTags and ClassTags.?

📕 Buy this interview preparation book: 100 Scala questions & answers — PDF + EPUB for $5

Type erasure is a concept in the Scala programming language (and in Java as well) where the JVM erases type information from objects during runtime. This is done mainly for backward compatibility reasons with older versions of Java. Due to type erasure, certain information about generic classes or methods is not available at runtime, which can lead to limitations and challenges when working with generic types in your code.

For example, let’s consider the following code snippet:

def genericMethod[T](list: List[T]): Unit = {
  if (list.isInstanceOf[List[String]]) 
    println("List of Strings")
  else if (list.isInstanceOf[List[Int]]) 
    println("List of Integers")
}

val stringList = List("hello", "world")
val intList = List(1, 2, 3)

genericMethod(stringList) // It should print "List of Strings"
genericMethod(intList)    // It should print "List of Integers"

In the code snippet, we have a generic method ‘genericMethod‘ which intends to check the type of the list and print the corresponding message. However, due to type erasure, both checks will return false, and nothing will be printed for both method calls.

To work around type erasure, Scala provides ‘TypeTag‘ and ‘ClassTag‘ which are used to carry type information at runtime. They allow you to store and retrieve the necessary type information even after type erasure.

‘TypeTag‘ is more powerful than ‘ClassTag‘ since it holds complete information about the type, including generic arguments. On the other hand, ‘ClassTag‘ provides less information and is mainly utilized in cases where only the main class information is needed, such as creating arrays of given types.

Here’s an example of using ‘TypeTag‘ and ‘ClassTag‘ to work around type erasure:

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

def genericMethodWithTypeTag[T : TypeTag](list: List[T]): Unit = {
  if (typeOf[T] =:= typeOf[String])
    println("List of Strings using TypeTag")
  else if (typeOf[T] =:= typeOf[Int])
    println("List of Integers using TypeTag")
}

def genericMethodWithClassTag[T : ClassTag](list: List[T]): Unit = {
  if (classTag[T].runtimeClass == classOf[String])
    println("List of Strings using ClassTag")
  else if (classTag[T].runtimeClass == classOf[Int])
    println("List of Integers using ClassTag")
}

genericMethodWithTypeTag(stringList) // It prints "List of Strings using TypeTag"
genericMethodWithTypeTag(intList)    // It prints "List of Integers using TypeTag"

genericMethodWithClassTag(stringList) // It prints "List of Strings using ClassTag"
genericMethodWithClassTag(intList)    // It prints "List of Integers using ClassTag"

In the modified code, we have created two methods (‘genericMethodWithTypeTag‘ and ‘genericMethodWithClassTag‘) that take advantage of ‘TypeTag‘ and ‘ClassTag‘ to retain the necessary type information at runtime. Now, these methods work as expected and print the correct messages based on the type of the given list.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Scala interview — then scores it.
📞 Practice Scala — free 15 min
📕 Buy this interview preparation book: 100 Scala questions & answers — PDF + EPUB for $5

All 100 Scala questions · All topics