Skip to content

Instantly share code, notes, and snippets.

@britter
Last active March 9, 2017 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save britter/8a609e7a4ffc2bd7c67f1fae0c2e0f5a to your computer and use it in GitHub Desktop.
Save britter/8a609e7a4ffc2bd7c67f1fae0c2e0f5a to your computer and use it in GitHub Desktop.
Closing over the enclosing scope using Props
final class ParentActor extends Actor {
private var value: Int = 0
override def receive = {
case Increment => value++
case CreateChild => context.actorOf(Props(new ChildActor(value)))
}
}
@britter
Copy link
Author

britter commented Mar 9, 2017

In this example we want to create a ChildActor with the current value from ParentActor. Usually we would expect that the ChildActor is created with the value at the time the CreateChild message is received. However, since value is passed by-name to Props.apply the ParentActor may already have received additional Increment messages before the by-name parameter is actually evaluated causing the new ChildActor to be created with a different value than expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment