Search code examples
akkaakka-actor

Akka static usage


I have a simple actor system with default configuration.

I have a class extended Actor

class Test extend Actor {
  def receive: Receive = {
      case Foo(collection) => sender ! extract(collection)
  }

  private def extract(c: List[FooItem]): List[BarItem] = ???
}

This actor have a companion object

object Test {
  def props: Props = ???
}

Is there are safe to do function extract like this:

object Test {
  def props: Props = ???
  def extract(c: List[FooItem]): List[BarItem] = ???
}

and use from another Actor ?


Solution

  • Yes, it's ok to define a method on a companion and then import and use that method in an actor class. Something like this would work just fine:

    object Test {
      def props: Props = Props[Test]
      def extract(c: List[FooItem]): List[BarItem] = {
        . . .
      }
    }
    
    class Test extend Actor {
      import Test._
      def receive: Receive = {
        case Foo(collection) => sender ! extract(collection)
      }
    }