newps | 9 Oct 02:49

Actors and mutable instance variables


I just learned that it is is OK to have side-effects in the following
instance, since the side-effect is contained within the actor.  Is this good
practice?  Or should message be passed as an argument to loop (in a
recursive fashion) inside act?

import scala.actors.Actor
import scala.actors.Actor._

case class SetMessage(msg: String)
case class GetMessage

object Foo extends Actor {
  private var message: String = ""

  def act = {
    loop {
      react {
        case SetMessage(msg) => message = msg
        case GetMessage => reply(message)
      }
    }
  }

  start
}

object Bar extends Application {
  Foo ! SetMessage("hi")
  println(Foo !? GetMessage)
(Continue reading)

newps | 9 Oct 03:01

Re: Actors and mutable instance variables


Sorry, I just realized I posted this in scala instead of scala-users.  I am
moving this to scala-user.

newps wrote:
> 
> I just learned that it is is OK to have side-effects in the following
> instance, since the side-effect is contained within the actor.  Is this
> good practice?  Or should message be passed as an argument to loop (in a
> recursive fashion) inside act?
> 
> import scala.actors.Actor
> import scala.actors.Actor._
> 
> case class SetMessage(msg: String)
> case class GetMessage
> 
> object Foo extends Actor {
>   private var message: String = ""
> 
>   def act = {
>     loop {
>       react {
>         case SetMessage(msg) => message = msg
>         case GetMessage => reply(message)
>       }
>     }
>   }
> 
>   start
(Continue reading)


Gmane