mardi 26 juillet 2016

Is there a way to type-cast a Scala object using reflection?

I am using reflection to create Scala objects. I don't have ready access to 'T', to type-cast an object like one normally would like this:

def makeOne[T](o:Object) = o.asInstanceOf[T]

I do have a universe.Type tho. Earlier in my processing I do something like this:

def analyze[T](o:Object)(implicit tt:TypeTag[T]) = {...}

I save tt.tpe, so much later when I'm creating an object of type T, I've lost the 'T' but I still retain tt.tpe.

Is there any way I can use this universe.Type to type-cast my object without having to need 'T', as in .asInstanceOf[T]?

(BTW, I can create the object using reflection just fine...Its just that the created object doesn't know its type. It's just thinks its an Any.)





Aucun commentaire:

Enregistrer un commentaire