samedi 20 juillet 2019

How to type cast Any dynamically in scala?

I want to convert "Any" object into an object of runtime type. Based on the class name (string) at the runtime, how do I convert an ANY object to actual object?

I tried using converting class name into class object using Class.forName

val clazz = Class.forName("my.package.Animal")
val any: Any = Animal(1, "simba")
any.asInstanceOf[clazz] // Compilation Error // Looking for a solution





Aucun commentaire:

Enregistrer un commentaire