Skip to content

Instantly share code, notes, and snippets.

@elkorn
Created January 20, 2015 19:14
Show Gist options
  • Save elkorn/a7335a85f91cd0d20d4c to your computer and use it in GitHub Desktop.
Save elkorn/a7335a85f91cd0d20d4c to your computer and use it in GitHub Desktop.
Typed actor concept
class IntStringActor extends TypedActor[Int,String] {
private var state: Int = 0
override def work(msg: Int): String = state.toString
override def respondWith(msg: String)(sender: Sender): Unit = sender ! msg
override def receiveMessage(msg: Int): Unit =
state = state + msg
}
trait TypedActor[+In, +Out] extends Actor with TakesInput[In] with ProducesOutput[Out] with Maps[In,Out] {
def receive: Receive = {
case msg: In => receiveMessage(msg)
respondWith(work(msg))(sender())
}
}
trait Maps[-A,+B] {
def work(msg: A): B
}
trait TakesInput[-A] {
def receiveMessage(msg: A)
}
trait ProducesOutput[-B] {
def respondWith(msg: B)(sender: Sender)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment