I noticed some gap around reflection and generics and I wonder is anybody can help here.
Let say I have the following abstract class:
abstract class Animal[T : Eatable: ClassTag] {
def eat(food: T) : Unit
}
And here is one implementation of it:
class Dog[DogFood] {
def eat (food: DogFood) : Unit = ???
}
class DogFood extends Eatable
And here i had some method that given an animal, feeds it
def feed[T <: Eatable](animal: Animal[T]) : Unit = {
val isAvailable = Storage.isAvailable[T]
???
}
Now let say I want to instantiate the implementation class using reflection as it is part of my system configuration:
val config = ....
val animalCls = config.getString("animal.type")
val animalInstance = animalCls = Class.forName(animalCls).newInstance
// Issue is here - I can't call this method as it uses classTags!
feed(animalInstance)
A solution might be :
val animalCls = config.getString("animal.type").asIsntanceOf[Animal[_]]
But then the following call inside feed will fail as it doesn't bind the right type:
Storage.isAvailable[T]
Any ideas here?
Thanks!
Aucun commentaire:
Enregistrer un commentaire