Description
This issue was originally filed by domi...@google.com
I would like to suggest the addition of 'readonly' as a keyword to complement 'final'. It's use should instruct the compiler to enforce the deep immutability of the variable it describes. This is useful to people using an API, and provides strong hints for optimization possibilities for the compiler and VM.
For example, this is legal:
Bar mybar;
foo(mybar);
void foo(final Bar bar) {
bar.thing = other_thing;
}
I suggest that:
Bar mybar;
foo(mybar);
void foo(readonly Bar bar) {
bar.thing = other_thing;
}
should be illegal and should error during compilation when assignment to bar.thing is attempted.
Similarly, any attempt to take a non-readonly reference to a readonly variable should be illegal.
This may require methods on an object to be marked as readonly to indicate that they don't mutate |this|, though it may be possible to statically determine this.