Scala programmer should have known that this sort of writing :
class Person{
var id = 0
}
var p = new Person
p.id
p.id = 2
equals to
class Person{
private var _id = 0
def id = _id
def id_=(i: Int) = _id = i
}
val p = new Person
p.id // be equal to invoke id method of class Person
p.id = 2 // be equal to p.id_=(2)
in effect. But if you comment the getter method def id = _id
, p.id = 2
will cause a compilation error, saying
error: value key is not a member of Person
Could anyone explain why ?
The compiler is so because the specification says so.
See the Scala Reference, p. 86, §6.15 Assignments.
Note that nothing prevents you from:
private
def id(implicit no: Nothing)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With