You can avoid the asInstanceOf
, but the advantages are limited -- the code becomes more idiomatic and you can make the ClassCastException
more specific:
def any(xml: String): Any = xml
def fromXML[T: ClassTag](xml: String): T = any(xml) match {
case a: T => a
case other => throw new RuntimeException("Invalid type " + other.getClass + " found during marshalling of xml " + xml)
}
On the other hand, this is more verbose and probably less efficient than the asInstanceOf
call.