IssueRaiser

Stable getters

# Stable Getters Promotion of fields is probably the most prominent request we have in response to the release of null safety. One approach to improve support for promotion could be to ensure that a particular getter will return the same value if invoked multiple times, that is, the getter is _stable_. Note that the underlying concept is _immutability_: A stable getter will implement an immutable property of the given object (if it's an instance getter, otherwise it's just an immutable property). This is a concept which is useful both because it enables promotion, but also for a number of other reasons related to software correctness and readability. ```dart abstract class A { stable num get n; } class B1 implements A { final num n; // OK. B(this.n); } class B2 implements A { late final int n = aComplexExpression; // OK. } class B3 implements A { num get n => someExpression; // Error, unless `someExpression` is stable. } ``` The modifier `stable` can be specified on any final instance variable or instance getter declaration. When `stable` is present in a declaration where the syntax also admits `final`, `final` is implied and may be omitted. A compile-time error occurs if an instance getter declaration directly or indirectly overrides another getter declaration, where each of them can be explicitly declared or implicitly induced by a variable declaration, when the overridden declaration is marked `stable` and the overriding declaration is not a stable decla

Posted by eernstg on Mar 13, 2021, 11:40 AM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.3,000 issues0 comments

support (SaaS)

Stable getters

eernstgMar 13, 2021, 11:40 AM

# Stable Getters Promotion of fields is probably the most prominent request we have in response to the release of null safety. One approach to improve support for promotion could be to ensure that a particular getter will return the same value if invoked multiple times, that is, the getter is _stable_. Note that the underlying concept is _immutability_: A stable getter will implement an immutable property of the given object (if it's an instance getter, otherwise it's just an immutable property). This is a concept which is useful both because it enables promotion, but also for a number of other reasons related to software correctness and readability. ```dart abstract class A { stable num get n; } class B1 implements A { final num n; // OK. B(this.n); } class B2 implements A { late final int n = aComplexExpression; // OK. } class B3 implements A { num get n => someExpression; // Error, unless `someExpression` is stable. } ``` The modifier `stable` can be specified on any final instance variable or instance getter declaration. When `stable` is present in a declaration where the syntax also admits `final`, `final` is implied and may be omitted. A compile-time error occurs if an instance getter declaration directly or indirectly overrides another getter declaration, where each of them can be explicitly declared or implicitly induced by a variable declaration, when the overridden declaration is marked `stable` and the overriding declaration is not a stable decla

Comments (0)

No comments yet. Start the discussion.

Comment on this issue