简体   繁体   中英

What is the best way of avoiding type erasure in Scala/Akka when sending and receiving messages?

I have a system of Actors in Scala/Akka that send and receive messages other than simple primitives. If I have a receive case of something like:

(name: String, details: Map[String, List[Int])

The compiler gives warnings of type erasure. It seems to me that the simplest way of fixing this is to define case classes for every possible message in my system.

Is there a better/more correct/more elegant way of doing this?

Yes, go for case class es and case object s and also put them into actors companion objects.

object Printer {
   case class Print(name: String, details: Map[String, List[Int])
}

class Printer extends Actor {
  import Printer._

  def receive = {
     case Print(name, data) => println(data)
     case _ => println("I'm sry, what?")
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM