I have many classes of a common base trait. I have a function that needs to create a new instance of a instance of one of these classes, but which one is only known at runtime.
Is it possible to pass this class as a parameter and create an instance at runtime in Scala?
I’m not even sure that the answer would be the best approach but currently it’s the only thing I can think of.
You could pass the value thus:
to the method with signature
but this smells bad. You’re either looking at implementing a factory pattern, or better still you could use polymorphism and simply do:
since your functionality is predicated on what class is involved.