Where to define class classes when using Actors

I am taking the first steps using scala.

I created PhotosLoaderActorone that takes care of loading the image and storing it in the cache. For this, I will have CacheActorand DownloadActor.

My PhotosLoaderActorhas the following:

override def act() {
  loop {
    react {
      case (caller : Actor, photoToLoad:String) => { // bla bla }

I just found out what I can use case classesto 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 identify case classes? If I call PhotosLoaderActorfrom another package, will importing this actor also import case classes? What is the practice?

+3
source share
3 answers

I tried several different approaches and decided to place all the related messages in an object, usually called something like XyzProtocol.

For instance:

object PhotoProtocol {
  case class LoadImage(caller: Actor, photoToLoad: String)
  case class UpdateCache(photoToLoad: String, photo: Photo)
  case object ClearCache
}

:

  • , IDE, ScalaDoc, 1 XyzProtocol , .

  • , , (, ), .

  • , case actor. , case .

: (, , ), .

PS. . โ€‹โ€‹ Scala 2.10.

+10

, . , , .

package org.numbers
import akka.actor.Actor

sealed trait Numbers
case class Even(n: Int) extends Numbers
case class Odd(n: Int) extends Numbers

class NumberActor extends Actor {
  def receive = {
    case Even(n) => doEven(n)
    case Odd(n) => doOdd(n)
  }

  def doEven = ...
  def doOdd = ...
}

scala , . , , , , . case "" , , .

+1

If the actor is a singleton object, I would probably place it inside the object itself.

+1
source

All Articles