Skip to content

Value types (deep immutability) #501

Open
@DartBot

Description

@DartBot

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).core-mtype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions