Doesn't (didn't) Scala have automatically generated setters?

Posted by Malvolio on Stack Overflow See other posts from Stack Overflow or by Malvolio
Published on 2011-02-05T14:09:00Z Indexed on 2011/02/05 15:25 UTC
Read the original article Hit count: 208

Filed under:

Google and my failing memory are both giving me hints that it does, but every attempt is coming up dry.

class Y {
    var y = 0
}
var m = new Y()
m.y_(3)

error: value y_ is not a member of Y

Please tell me I am doing something wrong. (Also: please tell me what it is I am doing wrong.)

EDIT

The thing I am not doing wrong, or at least not the only thing I am doing wrong, is the way I am invoking the setter. The following things also fail, all with the same error message:

m.y_ // should be a function valued expression
m.y_ = (3) // suggested by Google and by Mchl
f(m.y_) // where f takes Int => Unit as an argument
f(m.y)  // complains that I am passing in Int not a function

I am doing this all through SimplyScala, because I'm too lazy and impatient to set up Scala on my tiny home machine. Hope it isn't that...

And the winner is ...

Fabian, who pointed out that I can't have a space between the _ and the =. I thought out why this should be and then it occurred to me:

The name of the setter for y is not y_, it is y_= !

Observe:

class Y {
     var y = 0
}
var m = new Y()
m.y_=(3)
m.y
res1: Int = 3
m.y_=
error: missing arguments for method y_= in class Y;
follow this method with `_` if you want to treat 
it as a partially applied function
       m.y_=
         ^
m.y_=_
res2: (Int) => Unit = 
def four(f : Int => Unit) = f(4) 
four(m.y_=)

m.y
res3: Int = 4

Another successful day on StackExchange.

© Stack Overflow or respective owner

Related posts about scala