Skip to content

Instantly share code, notes, and snippets.

@bigtoast
Created May 23, 2012 00:19
Show Gist options
  • Save bigtoast/2772493 to your computer and use it in GitHub Desktop.
Save bigtoast/2772493 to your computer and use it in GitHub Desktop.
Leaking mutable state into a future..
import akka.actor._
import akka.dispatch._
import akka.pattern.pipe
trait StuffService {
type Stuff
def doStuff :Future[Stuff]
}
case object DoStuff
class LeakyLeaksalot( service :StuffService ) extends Actor {
import StuffService._
var currentStuff :List[Stuff] = Nil
def receive = {
case DoStuff => // bad
service.doStuff.map { sender ! _ }
case DoStuff => // better
val replyTo = sender
service.doStuff.map { replyTo ! _ }
case DoStuff => // best
service doStuff pipeTo sender
case DoStuff => // very bad. local mutable var leaked into future block
service doStuff map { currentStuff :+ _ }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment