Scala variable binding when used with Actors

自闭症网瘾萝莉.ら 提交于 2019-12-23 10:10:39

问题


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)


来源:https://stackoverflow.com/questions/8441102/scala-variable-binding-when-used-with-actors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!