Hello Peter,
One idea you might give a spin is to extract actor behaviors into traits, 
and compose behavior of a concrete actor using those.
This can work because `receive` is basically just a `PartialFunction[Any, 
Unit]`, and those have a nice method called 
`PartialFunction#orElse(PartialFunction): PartialFunction`

In code it might look something like this:

```
trait ProducerBehavior { 
  this: Actor =>

  val producerBehavior: Receive = { ... }
}

trait ConsumerBehavior { 
  this: Actor =>

  val consumerBehavior: Receive = { ... }
}

class PActor extends Actor with ProducerBehavior {
  def receive = producerBehavior
}

class CActor extends Actor with ConsumerBehavior {
  def receive = consumerBehavior
}


class PCActor extends Actor with ProducerBehavior with ConsumerBehavior {
  def receive = producerBehavior orElse consumerBehavior
}
```

Not sure about your precise use-case, but code wise, using this you're able 
to duplicate as little as possible :-)

-- 
Konrad
http://geecon.org

W dniu wtorek, 18 lutego 2014 20:33:23 UTC użytkownik Peter Wolf napisał:
>
> Hello, there must be a document or tutorial about this somewhere, but I 
> can't find it...  How do Actors implement message polymorphism?
>
> I want to make a system where some Actors are "Producers", some are 
> "Consumers" and some are both "Producers and Consumers"
>
> My actual code is more complicated, so here is a stripped down version to 
> concentrate on my question
>
> Producers accept the message "GimmeStuff" and respond by sending "Stuff". 
>  Consumers send the message "GimmeStuff" and accept "Stuff"
>
> Here they are
>
> abstract class Producer[WORK] extends Actor {
>   def receive = {
>     case GimmeStuff =>
>        if(stuffAvailable) sender ! Stuff(nextStuff) 
>
> abstract class Consumer[WORK] extends Actor {
>   def receive = {
>     case Stuff(stuff) =>
>       doStuff(stuff)
>       sender ! GImmeStuff
>
> How do I implement ProducerAndConsumer without duplicating the receive 
> code?
>
> Thanks
> P
>
>

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: http://akka.io/faq/
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to