9 Oct 02:49
Actors and mutable instance variables
From: newps <fingermark <at> gmail.com>
Subject: Actors and mutable instance variables
Newsgroups: gmane.comp.lang.scala
Date: 2008-10-09 00:52:53 GMT
Subject: Actors and mutable instance variables
Newsgroups: gmane.comp.lang.scala
Date: 2008-10-09 00:52:53 GMT
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)
RSS Feed