10

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 {
   //....
   val res = List(1, 2, 3) map { x => context actorOf Props(new T(x)) } // error
}

abstract class Class2[U <: Class3 : ClassTag](a: Int) extends Actor { ... }
abstract class Class3(b: Int) extends Actor

But there is an error saying class type required but T found.

Alan Coromano
  • 24,958
  • 53
  • 135
  • 205

3 Answers3

10

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)) }
}
senia
  • 37,745
  • 4
  • 88
  • 129
  • is there any way to specify that T must a constructor like in C# : `where T: new()`? – Alan Coromano Dec 15 '13 at 09:48
  • @MariusKavansky, there is no such way. Actually in `C#` it's a syntax sugar for additional `Func` parameter (just like in my second code sample). – senia Dec 15 '13 at 09:50
  • you perhaps meant def in `abstract createT(i: Int): T`? – Alan Coromano Dec 15 '13 at 13:15
  • When I implement `def createT(i: Int): T` in a child class (using a concrete class instead of T), it says "type mismatch: found : MyConcreteClass, required Nothing". Do you have any idea why is this? – Alan Coromano Dec 15 '13 at 13:18
  • @MariusKavansky: I guess you didn't specify type parameter `T`. Could you please show you code sample (using pastebin)? – senia Dec 15 '13 at 13:21
  • it's hard of hard because I have much more complex class hierarchy. – Alan Coromano Dec 15 '13 at 13:39
5

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]()
J Cracknell
  • 3,498
  • 1
  • 19
  • 13
1

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

user4298319
  • 432
  • 3
  • 10
  • In `C#` it's a compile time syntax sugar, it has nothing with type erasure. – senia Dec 15 '13 at 09:51
  • 1
    Yes. C# has true generics and they are runtime. – user4298319 Dec 15 '13 at 12:49
  • I mean there is no such syntax like `: new()` in `scala` and `java` not because of `type erasure` - it's a compile time mechanism and `scala` language developers could implement it on `jvm`. They just don't think it's necessary. – senia Dec 15 '13 at 12:58
  • 1
    In scala you can write [T: Manifest], and then use the implicit Manifest (or TypeTag for newer versions) to create a T by reflection. Google context bounds for further explanation. – Jürgen Strobel Mar 21 '14 at 12:41