In my scala code, I want to be able to instantiate a new class. For example, suppose I have the code below:
class Foo { def foo=10 } trait Bar { val bar=20 }
Ideally, I want to do something like:
def newInstance[A <: Foo] = { new A with Bar } newInstance[Foo]
But of course this will not work. I tried using reflection to create an instance of the class, but it seems that I can only create a new class (and not mix with the attribute). I think it would be possible to do this work with macros, but I'm not sure where to start.
What I'm trying to do is similar to the following Ruby code:
class SomeClass def create self.class.new end end class Other < SomeClass end Other.new.create
Is it possible?
macros reflection scala scala-macros
Maurรญcio Szabo
source share