dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.2k stars 1.57k forks source link

Should the `immutable` annotation disallow getters that override a (final) field? #54492

Open bwilkerson opened 9 months ago

bwilkerson commented 9 months ago

Consider the following example (from @eernstg):

import 'package:meta/meta.dart';

@immutable
class A {
  final int i;
  const A(this.i);
}

int _i = 0;

class B implements A {
  @override
  int get i => ++_i; // OK, fine!
}

If the intention of the annotation is to ensure that the values of fields don't change, then we might want to disallow overriding fields with getters (whether all getters of a subset of getters is an interesting discussion question).

eernstg commented 9 months ago

Sounds good! I think this is in line with a natural expectation for the meaning of @immutable (something like: If I evaluate an immutable property twice and get two results o1 and o2 then they are "the same" in some sense, e.g., identical(o1, o2) or o1 == o2 && o2 == o1).