Reputation: 92066
Here's some code using Dynamic
. As you can see, the part below works as expected.
scala> import language.dynamics
import language.dynamics
scala> class ExpandoObject extends Dynamic {
| private val dict = collection.mutable.Map.empty[String, Any]
| def selectDynamic(name: String): Any = dict(name)
| def updateDynamic(name: String)(arg: Any) = dict(name) = arg
| }
defined class ExpandoObject
scala> val e = new ExpandoObject
e: ExpandoObject = ExpandoObject@14e03fec
scala> e.name = "rahul"
e.name: Any = rahul
scala> e.name
res62: Any = rahul
However when you use it in some block, it fails to work.
scala> {
| val e = new ExpandoObject
| e.name = "rahul"
| }
<console>:20: error: reassignment to val
e.name = "rahul"
^
scala>
Again, if you call updateDynamic
explicitly, it works.
scala> {
| val e = new ExpandoObject
| e.updateDynamic("name")("rahul")
| e
| }
res66: ExpandoObject = ExpandoObject@3f755bd2
scala> res66.name
res67: Any = rahul
Is this a bug? Or something I am simply missing?
Upvotes: 3
Views: 109
Reputation: 10667
This is a regression in earlier versions of 2.10
, before 2.10.1-RC1
. I was able to reproduce the issue in 2.10.0
, then ran the same code with 2.10.1
, and it worked without errors.
Upvotes: 3