I can\'t create an actor for some reason (here is a simple version of my class hierarchy):
abstract class Class1[T <: Class2[_]: ClassTag] extends Actor {
One approach I have used involves creating an 'instantiator' trait which can be used to create instances of types for which an implicit instantiator exists:
trait Instantiator[+A] {
def apply(): A
}
object Instantiator {
def apply[A](create: => A): Instantiator[A] = new Instantiator[A] {
def apply(): A = create
}
}
class Foo() { ... }
object Foo {
implicit val instantiator: Instantiator[Foo] = Instantiator { new Foo() }
}
// def someMethod[A]()(implicit instantiator: Instantiator[A]): A = {
def someMethod[A : Instantiator](): A = {
val a = implicitly[Instantiator[A]].apply()
...
}
someMethod[Foo]()
You can't call new T(x)
with type parameter T
. There could be no such constructor for T
:
class WithoutSuchConstructor extends Class2[Class3](1)
You should specify method to create T
explicitly:
abstract class Class1[T <: Class2[_]: ClassTag] extends Actor {
//....
def createT(i: Int): T
val res = List(1, 2, 3) map { x => context actorOf Props(createT(x)) }
}
Alternatively:
abstract class Class1[T <: Class2[_]: ClassTag](createT: Int => T) extends Actor {
//....
val res = List(1, 2, 3) map { x => context actorOf Props(createT(x)) }
}
I think this is because of JVM restriction also known as "type erasure". http://docs.oracle.com/javase/tutorial/java/generics/erasure.html
also see "Cannot Create Instances of Type Parameters" at http://docs.oracle.com/javase/tutorial/java/generics/restrictions.html
By the way C# allows you to write:
new T()
when you define a restriction
where T: new()
but unfortunately constructor must be parameterless