Themerius
Themerius

Reputation: 1911

using scala generics in function definition

I'd like to build a convinient function for making akka actors:

 def makeActor[T](id: Int): ActorRef =
   system.actorOf(Props(classOf[T], id, updater), "entity" + id)

 makeActor[SomeActor](1) ! "Some Message"

But this says "class type required but T found". Whats wrong with this code?

Upvotes: 0

Views: 105

Answers (3)

4lex1v
4lex1v

Reputation: 21557

Use scala reflection for this, e.g:

import scala.reflect._

def spawn[T <: Actor: ClassTag](id: Int) = system.actorOf(Props(classTag[T].runtimeClass, ...), s"entity$id")

Upvotes: 3

yǝsʞǝla
yǝsʞǝla

Reputation: 16412

Fully working example:

import akka.actor.{ActorSystem, Props, Actor, ActorRef}
import scala.reflect.ClassTag

object TestActor extends App {

  val system: ActorSystem = ActorSystem("rest-service-actor-system")

  def makeActor[T <: Actor : ClassTag](id: Int): ActorRef =
    system.actorOf(Props(implicitly[ClassTag[T]].runtimeClass), "entity" + id)

  class A extends Actor {
    override def receive = {
      case anything => println("A GOT MESSAGE: " + anything)
    }
  }

  class B extends Actor {
    override def receive = {
      case anything => println("B GOT MESSAGE: " + anything)
    }
  }

  makeActor[A](1) ! "hello"
  makeActor[A](2) ! "bye"

  system.shutdown()
}

usually prints:

A GOT MESSAGE: bye
A GOT MESSAGE: hello

It forces you to have a type that is also an Actor. For instance this code will not compile:

class C

makeActor[C](3)

error you get is:

type arguments [TestActor.C] do not conform to method makeActor's type parameter bounds [T <: akka.actor.Actor]
  makeActor[C](3)
           ^

Upvotes: 2

Rado Buransky
Rado Buransky

Reputation: 3294

An answer can be found here: Scala classOf for type parameter

You can write your code for example like this:

def makeActor[T](id: Int)(implicit ev: Manifest[T]): ActorRef = system.actorOf(Props(ev.erasure, id, updater), "entity" + id)

Upvotes: 2

Related Questions