Last active
March 9, 2017 15:53
-
-
Save britter/8a609e7a4ffc2bd7c67f1fae0c2e0f5a to your computer and use it in GitHub Desktop.
Closing over the enclosing scope using Props
This file contains 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
final class ParentActor extends Actor { | |
private var value: Int = 0 | |
override def receive = { | |
case Increment => value++ | |
case CreateChild => context.actorOf(Props(new ChildActor(value))) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In this example we want to create a
ChildActor
with the currentvalue
fromParentActor
. Usually we would expect that theChildActor
is created with thevalue
at the time theCreateChild
message is received. However, sincevalue
is passed by-name toProps.apply
theParentActor
may already have received additionalIncrement
messages before the by-name parameter is actually evaluated causing the newChildActor
to be created with a differentvalue
than expected.