31 Computed properties
A computed property presents accessor behavior with field-like syntax.
norm
class Counter {
Integer stored
Counter(Integer initial) {
stored = initial
}
Integer value {
get { return stored }
set(next) { stored = next }
}
}
main() {
Counter counter = Counter(initial: 2)
counter.value = 5
printLine(counter.value)
}Expected output:
text
5stored is the backing field. Reading value invokes get, while assigning it invokes set(next); the property itself occupies no separate stored field. Remove the setter to make this property read-only.
Try it: Make the setter add one and observe the later read.
Precise rules: Reference.