-
Notifications
You must be signed in to change notification settings - Fork 214
[nnbd] Late Keyword is Redundant for Local Non-nullable variables without Intializers #1101
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
That is not quite true: A With a non-late variable it is an error to read it unless it is guaranteed to be initialized on all control-flow paths. For example: void main() {
bool b = true;
late int i;
if (b) i = 1;
i; // OK with `late`, but compile-time error if `i` is not late.
} Of course, if we change that to The notion of |
@eernstg Yes, I said that it is very clear to me - just probably not very obvious for everyone 🙂 I see that there are edge cases, but a lint still makes sense because it can figure out when |
Oh, but everything is obvious to everyone, of course, at least if we look again and squint a bit. 😄 And I agree that it could be quite helpful to have a lint which flags local variables that are |
We debated extensively whether to use different keywords for late vs lazy or just one. You can see some of the discussion here. In the end we decided to go with one, for better or for worse. The subject of lint for unnecessary late was just raised here as well . cc @bwilkerson |
Uh oh!
There was an error while loading. Please reload this page.
Late assignment of non-nullable local variables
The above is strictly equivalent to the following:
This is because non-nullable variables also need to be assigned before first access. For nullable variables, the above does not apply because they can be accessed without explicit initialization.
Late lazy
Obviously, the
late
modifier is still useful for non-nullable local variables when they have initializers.This would be an argument for separating the keyword into two distinct modifiers (which is already the case under the hood as the two effects of the modifier are mutually exclusive).
Proposal
I think that any one of these actions is sensible given the above:
late
for non-nullable variables without initializers.late
modifier into two keywords, i.e.late lazy
andlate assign
.Why?
To me the two effects, or better two kinds, of the
late
modifier make perfect sense, however, I think that interactions like this will makelate
very confusing for people who spend less time with Dart.Related
#1100
The text was updated successfully, but these errors were encountered: