Where to define case classes when using Actors
actor, scala
Solution
I've tried a few different approaches and settled on putting all related messages in to an object, typically named something like XyzProtocol.
For example:
object PhotoProtocol {
case class LoadImage(caller: Actor, photoToLoad: String)
case class UpdateCache(photoToLoad: String, photo: Photo)
case object ClearCache
}
I like this approach for a few reasons:
Scanning the containing package, either in an IDE or in ScalaDoc, shows only 1 or a few XyzProtocol objects instead of being littered with dozens of messages.
If the number of actors required to process a message changes over time (e.g., introduction of an actor pool or indirection to another actor subtree), the messages are not impacted.
There's no accidental capture of the actor instance, as is there when defining the case classes inside the actor class. This accidental capture is reason enough for me to never define the case classes inside the actor class.
Note: if the message is purely an internal implementation detail of an actor (e.g., an internal signal sent to itself by itself), I define the message in the companion object of the actor class.
PS. I also suggest moving to Akka from standard library actors. Akka will be added to Scala 2.10 distribution.
Problem
I am giving my first steps with `scala`. I have created an `PhotosLoaderActor` that will take care of downloading an image and saving it into a cache. To do this, I will have a `CacheActor` and a `DownloadActor`. My `PhotosLoaderActor` has this: ``` override def act() { loop { react { case (caller : Actor, photoToLoad:String) => { // bla bla } ``` I just learned I could use `case classes` to use something like this: ``` case class LoadImage(caller: Actor, photoToLoad: String) override def act() { loop { react { case LoadImage(caller, photoToLoad) => { // bla bla } ``` My question is: Where should I define the `case classes`? If I am calling the `PhotosLoaderActor` from a different package, importing the actor would also import the `case classes`? Which is the best practice?