Can you return an assignable lvalue in Scala?
- by Alex R
(note, lvalue is actually a term from the C grammar, I don't know what it's called in Scala!)
Trying to learn Scala... this evening I'm working on an internal DSL for a dynamically scoped language that might resemble PHP syntax.
My REPL is: Welcome to Scala version 2.7.6.final (Java HotSpot(TM) Client VM, Java 1.6.0).
I have some made-up example code:
class $(any: Any) {
def update(sym: Symbol, any: Any) { println("line 2 executed");}
def -(sym: Symbol) : $ = { println("line 1 executed"); return this }
def update(any: Any) { println("line 3 executed");}
}
The following works as expected:
scala var a = new $(0)
a: $ = $@19238ad
scala a('x) = "blah"
line 2 executed
On the other hand, why does the following not invoke the 1-parameter update method?
scala a = 1
:6: error: type mismatch;
found : Int(1)
required: $
a = 1
^
Ultimately, I would like this to work:
a-'x = "blah"
Thanks