Scala variable binding when used with Actors

2019-06-21 17:44发布

问题:

I am fairly new to Scala. I am trying to understand how/if scala does dynamic binding when a closure is passed as part of a message to an Actor.

I am using Akka 1.2 with Scala 2.9.

I have the following code segment (modified from http://gleichmann.wordpress.com/2010/11/15/functional-scala-closures/)

var minAge = 18
val isAdult = (age: Int) => age >= minAge

actor ! answer(19, isAdult) 
minAge = 20
actor ! answer(19, isAdult)

On the actor side, it simply applies isAdult to the first parameter and prints the result. Since Scala uses dynamic binding (so I was told), I would have expected

true
false

but somehow the result is

false
false

So is it true that scala is binding the variable statically and taking 18 as the value of minAge for both answer messages? Is there a way to keep the dynamic binding behavior while using closures in messages?

Thanks!

回答1:

Your understanding of closures is correct. It is doing dynamic binding however you are introducing concurrency issues. The closure is binding to mutable data which makes the closure mutable. The actor model only solves concurrency issues when you pass strictly immutable data between actors. You may write it in a seemingly chronological order but the actor scheduler changes the order of events. And since isAdult is mutable the reordering changes the results.

actor ! answer(19, isAdult) // message put in actor queue
minAge = 20
actor ! answer(19, isAdult) // message put in actor queue
// later...
// actor handles answer(19, isAdult)
// actor handles answer(19, isAdult)