I'm trying to create an instance of a trait using this method
val inst = new Object with MyTrait
This works well, but I'd like to move this creation in to a generator function, ie.
object Creator {
def create[T] : T = new Object with T
}
I'm obviously going to need the manifest to somehow fix the type erasure problems, but before I get to this, I run in to 2 questions :
Even with an implicit manifest, Scala still demands that T be a trait. How do I add a restriction to create[T] so that T is a trait?
If I chose to use the Class.newInstance method to create the instance dynamically rather than using "new", how would I specify the "with" in "new Object with T"? Is it possible to dynamically create new concrete mixin types at runtime?
You can't do this (even with a Manifest). The code
new Object with T
involves creating a new anonymous class representing the combination ofObject with T
. To pass this to yourcreate
function, you would have to generate this new class (with new bytecode) at runtime, and Scala has no facilities for generating a new class at runtime.One strategy might be to try to transfer the special functionality of the factory method into the class's constructor instead, and then use the constructor directly.
Another possible strategy is to create conversion functions (implicit or otherwise) to the traits you're interested in using with this class.
I'm not sure what the motivation is for your question, but you could consider passing a factory for
T
as an implicit parameter. This is known as using type classes or ad-hoc polymorphism.At the other end of the spectrum, you could invoke the compiler at runtime, as detailed in this answer to the question "Dynamic mixin in Scala - is it possible?".