Class type required but T found

前端 未结 3 2001
误落风尘
误落风尘 2020-12-10 15:16

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 {         


        
3条回答
  •  执念已碎
    2020-12-10 15:32

    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)) }
    }
    

提交回复
热议问题