Created
January 20, 2015 19:14
-
-
Save elkorn/a7335a85f91cd0d20d4c to your computer and use it in GitHub Desktop.
Typed actor concept
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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