The following example shows a way to cast a value in Scala. In this example, one of the java utility methods called “getNetworkInterfaces” class returns something we want cast to a more specific class, a NetworkInterface:
val e = NetworkInterface.getNetworkInterfaces
while(e.hasMoreElements) {
val networkInterface = e.nextElement match {
case e: NetworkInterface => e
case _ => ???
}
// process this network adapter
}
Once the “match” evaluates, the “networkInterface” variable will have the desired type.
If the cast fails, we throw NotImplementedException (??? is a shortcut for this). You could alternately try different casts by matching on other patterns.