-
Notifications
You must be signed in to change notification settings - Fork 214
Make flow control treat final vars immutable #4131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
pretty sure that's an edgecase that hasn't been considered. AFAIK, the reason why members are not promoted is because of possible sideeffects that can occur. Still, i don't see a reason not to promote it, since the immutability means that there are no sideeffects, even when working in async. |
If you try to subclass C and make |
I didn't consider this. Perhaps we should allow promoting in sealed classes, where non of the subclasses change the mutability of the variable. I know that currently this code doesn't work (tested in dartpad): sealed class SuperClass {
final int? i;
const SuperClass(this.i);
}
class ChildClass extends SuperClass {
int? b;
// variable i is still final
ChildClass(super.i, this.b);
}
void f(SuperClass c) {
late int i;
if (c.i != null) {
i = c.i;
}
} AFAIK, |
Normally, similar requests (to implement something based on "holistic" knowledge) were rejected because such logic would be brittle (if subclasses don't exist today, it doesn't mean they won't emerge tomorrow). For this promotion to become feasible, |
What @tatumizer says. That's why anything that would treat the two differently must be opt-in. Being a final class only affects the current program. We can know that nobody overrides the variable with something non-stable, but we still don't know if the author intended to promise that the property will always be stable. |
@tatumizer hope this will come true one day. |
@hydro63 I'd say, this has to be default behavior even for non-sealed. At least, subclasses can be allowed to introduce immutability, but not ignore it. |
Here is sample code:
Despite the var

i
is final, the container class is declared@immutable
and there is explicitif (c.i != null)
check, the IDE considers this code invalid:Wouldn't it be possible to promote nullable vars to non-nullable implicitly inside the
if
block in such cases?The text was updated successfully, but these errors were encountered: