Reassignment sugar fails when getter is defined in super-trait

21 views
Skip to first unread message

Hanns Holger Rutz

unread,
Aug 12, 2012, 5:40:20 PM8/12/12
to scala...@googlegroups.com
I have a read-only and mutable variant of a data structure:

trait Config {
def method: Int
}
trait ConfigBuilder extends Config {
def method_=( value: Int ) : Unit
}

now I was expecting that the following works:

def test( b: ConfigBuilder ) {
b.method += 33 // fails
}

... but it doesn't :

<console>:10: error: reassignment to val
b.method += 33
^

If I _restate_ the contents of Config:

trait ConfigBuilder extends Config {
def method: Int
def method_=( value: Int ) : Unit
}

... it does compile

def test( b: ConfigBuilder ) {
b.method += 33 // ok
}

this kind of sucks with a dozen configuration keys. Is this some oversight in scalac or is there a deeper reason by the reassignment possibility is not seen in the first case?

best, .h.h.

Reply all
Reply to author
Forward
0 new messages