Created
May 23, 2012 00:19
-
-
Save bigtoast/2772493 to your computer and use it in GitHub Desktop.
Leaking mutable state into a future..
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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