To answer the last two questions of the OP: using a `var sum: Int` saves
one object allocation per processed message, which could be significant
depending on the use-case. Due to the duties of `def receive` being to
return the initial behavior the Actor you present is structurally what
needs to be written when using the `become` approach, but I wouldn’t call
that method `morph` ;-) (I usually pick a name that describes the behavior
that it returns.)

Regards,

Roland

On Tue, Aug 11, 2015 at 8:53 PM, Richard Rodseth <rrods...@gmail.com> wrote:

> I sometimes use a single var containing an immutable model. Remember that
> you don't have to worry about synchronization and locks, so multiple vars
> may not be the end of the world.
> I also use the become(state) technique.
> Also look into the FSM trait.
> http://doc.akka.io/docs/akka/snapshot/scala/fsm.html
>
> On Mon, Aug 10, 2015 at 7:16 AM, <lalge...@gmail.com> wrote:
>
>> Actors typcially required state that are var-declarations.; e.g., to hold
>> a sum in an accumulator. What's the best way to do this without
>> var-declarations?
>>
>> One that comes to mind is to keep 'becoming' a new behavior as shown
>> below. Is this efficient? Is there a pattern to write the *receive *method
>> without the auxillary *morph *method?
>>
>> Enter code here...
>> class FunctionalActor(val sum: Int) extends Actor {
>>     def morph(sum: Int) : Receive = {
>>         case "inc" =>
>>             context.become(morph(sum+1))
>>         case "report" =>
>>             sender() ! sum
>>             context.become(morph(0))
>>     }
>>
>>     def receive : Receive = morph(sum)
>> }
>>
>> Thanks.
>>
>> Lal
>>
>> --
>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>> >>>>>>>>>> Check the FAQ:
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>> >>>>>>>>>> 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/d/optout.
>>
>
> --
> >>>>>>>>>> Read the docs: http://akka.io/docs/
> >>>>>>>>>> Check the FAQ:
> http://doc.akka.io/docs/akka/current/additional/faq.html
> >>>>>>>>>> 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/d/optout.
>



-- 
Akka Team
Typesafe - Reactive apps on the JVM
Blog: letitcrash.com
Twitter: @akkateam

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      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/d/optout.

Reply via email to