Each setter method requires a getter method in Scala?

The Scala programmer should have known this kind of entry:

class Person{ var id = 0 } var p = new Person p.id p.id = 2 

equally

 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) 

. But if you comment on the getter def id = _id , p.id = 2 will result in a compilation error saying

 error: value key is not a member of Person 

Can anyone explain why?

+6
source share
1 answer

The compiler is like that because the specification says so.

See Scala Help , p. 86, ยง6.15. Destination.

Please note: nothing bothers you:

  • private getter creation
  • so that the recipient returns another type
  • makes getters "unrecoverable," for example. as follows: def id(implicit no: Nothing)
+8
source

Source: https://habr.com/ru/post/912576/


All Articles