0

I have the following code, where T is a actor subclass that should also take a constructor argument:

abstract class AbstractActor(dest: ActorRef) extends Actor {
  //...
}

class ChildActor(dest: ActorRef) extends AbstractActor(dest) {
  //...
}

class ParentActor[T <: AbstractActor : ClassTag] extends Actor {
  val childRef = context.actorOf(Props(classOf[T], destActorRef))
  //...
}

The compiler gives the error: "class type required but T found". I assume the problem is that one could also define childActor without the constructor parameter:

class ChildActor extends AbstractActor(dest) {
  //...
}

So, I tried:

class ParentActor[T <: AbstractActor : ClassTag] extends Actor {

  def createT(dest: ActorRef)(implicit ev: Manifest[T]): ActorRef =
    context.actorOf(Props(ev.runtimeClass, dest))

  val childRef = createT(destActorRef)
  //...
}

But then I get: "no manifest available for T". Any ideas on what I'm doing wrong? Thank you

Lasf
  • 2,536
  • 1
  • 16
  • 35
  • For the "no manifest available for T" part, this thread may be helpful: http://stackoverflow.com/questions/7294761/why-is-the-manifest-not-available-in-the-constructor – Sudheer Aedama Jul 07 '14 at 22:55

1 Answers1

1

In the first attempt, your code fails because of classOf[T]. classOf needs to be given an explicit class, it can't work with a type parameter, even if it has a ClassTag.

In the second attempt, you require a ClassTag for type T but then createT requires a Manifest. Manifest is a subtype of ClassTag, and the ClassTag that is available cannot safely be downcast to Manifest. You are close though, change the requirement to [T <: AbstractActor : Manifest] and your code should work.

wingedsubmariner
  • 13,350
  • 1
  • 27
  • 52
  • Given that Manifest is apparently disappearing at some point in the future, see any pitfalls in replacing it with ClassTag? So we'd have `(implicit tag: ClassTag[T])` and then `Props(tag.runtimeClass, dest)` instead – Lasf Jul 08 '14 at 03:55
  • 1
    It looks like moving to `ClassTag` is the right choice. – wingedsubmariner Jul 08 '14 at 04:56