(note: lvalue is actually a term from C grammar, I don't know what it called in Scala!)
Trying to get to know Scala ... tonight I am working on an internal DSL for a dynamic reach language that may resemble PHP syntax.
My REPL: Welcome to Scala version 2.7.6.final (Java Virtual Machine HotSpot (TM), Java 1.6.0).
I have code with ready-made 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 the following does not call the update method with 1 parameter?
scala> a = 1
: 6: error: type mismatch;
found: Int (1)
required: $
a = 1
^
When doing some trial and error, I found this syntactic curiosity:
scala> class A {def this_ = {print ("hello")}}
defined class A
scala> var a = new A
a: A = A @ 9aca82
scala> a = 2
: 6: error: type mismatch;
found: Int (2)
required: A
a = 2
^
scala> a.this_
: 7: error: value this_ is not a member of A
a.this_
^
What is the meaning of overriding "this_" above? Where is he going?
Ultimately, I would like this to work:
a -> 'x = "blah"
thank
source
share