Setter doesn't write to backing field, but default getter - shouldn't get a warning?


#1

I’m thinking, it’s quite easy to accidentally create a situation when I have custom setter that doesn’t set default backing field - but if I leave default getter it will be generated using the backing field (so it will always return initial value - which will not even use setter)

For example
private var _price : Float = 20
var price : Float = 10.0
set(value) {
_price = value
}

Using “price” property will always return 10.0. This is understandable, but shouldn’t we get at least some warning about it? I would say if setter doesn’t access backing field, default getter either shouldn’t be allowed at all (as it doesn’t really make sense in such case) or at least trigger warning


#2

It is certainly something that a static analyser should warn about. The compiler could as well (it is quite trivial to check) and probably it should be an error despite having clear semantics (unlikely to be correct but not provably incorrect)