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
Can late
alone be used to declare a variable?
#321
Comments
I'm split here. My personal preference is to go with It's likely a moot point without an initializer expression because a A more interesting case is |
I agree. Given that |
I don't know if I have a coherent take on this, but here's a brain dump: Allowing it would make it sort of consistent with This dual nature was one of the sticking points with option semicolons: final veryLongIdentifier
anotherLongIdentifier // <-- Part of previous line or not? We get a lot of feedback from users that they don't like In something simple like: late x; Sure, maybe it's pretty obviously some kind of variable declaration thing because there's only a name after it. But if we add destructuring, it gets weird: late [x, y] = someFunction(); It's hard for me to look at that and not find it very opaque. (I don't know if we will support Note that allowing Also, you can use I am also worried that inferring late x;
late final x; // 6 characters longer. :( The way I think about features like this that introduce pretty novel (relative to the larger software world) behavior is that we should err on the side of explicitness. Users pick up syntax much quicker than semantics (ex: initializing formals versus library privacy). So if we have some new, interesting behavior, I want the syntax to be as simple as possible and telegraph it clearly. I think it's simple and clear to say "'late' is a modifier you can put before a variable declaration". One way to think about the cognitive load of this is to look at the grammar. Right now (simplified, ignore multi-variable declarations), the grammar is something like:
If we treat
If we allow it as a declarator or a modifier, I think it's:
That feels hairier to me once you expand out all the We don't treat class Foo {
static x = 123;
bar(covariant y) {}
} We also don't treat foo({required x}); But you can also do: foo({required var x}); (I don't know why we allow So I don't know if it's very compelling to say that I don't know if any of this is compelling, but I can't shake the feeling that treating |
This isn't actually how the grammar is (among other things, it ignores const), but even so, this is more complicated than it needs to be:
vs
I think the former is about as clear as the latter. |
I think also think we can allow `final late` without making the grammar any harder to parse. Even if parsers prefer a fixed order, seeing the first of `final` and `late` can make them use different paths for the rest.
Making the parser accept these modifiers in either order is trivial.
But in my mind the bigger question is whether we want to treat these
modifiers differently than other modifiers. The grammar currently specifies
a single order for modifiers everywhere else; as far as I know this would
be the first place the user is given a choice. You can't, for example,
write `final static int zero = 0;`. (And if we _do_ give users a choice,
the style guide will likely recommend a standard order.)
…On Fri, Apr 26, 2019 at 6:43 AM Lasse R.H. Nielsen ***@***.***> wrote:
I think also think we can allow final late without making the grammar any
harder to parse. Even if parsers prefer a fixed order, seeing the first of
final and late can make them use different paths for the rest.
declarator ::=
"var"
| type
| "final" "late"? type?
| "late" "final"? type?
(I still prefer final late over late final, but I'm willing to allow both
😄)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#321 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABMR5KZSQQGOBJPWLXNKAYTPSMBJJANCNFSM4HG7FFYQ>
.
|
We only allow one order, and the order is generally from more impactful to less. So For If you do final late String name;
final late int age;
if (something) {
name = ...
age = ...
} else {
name = ...
age = ...
} then the late-ness is very localized, and the remainder of the life-time of the varible should just treat it as final. That's why I think there isn't one answer which fits all use-cases. I expect the publicly late write-once case to be rare compared to the lazy initialization, and I prefer |
Just wondering, why to use 'late' rather than 'lateinit'? Most developers are comfortable with that already. |
A few reasons:
|
maybbe I would rather type something like |
Ok, per discussion:
|
We propose to add a keyword
late
for marking late initialized variables. This means that for the following declarations:there are corresponding
late
variable declarations as follows:What is the
late
declaration that corresponds to:var x;
Is it
or
late x;
The former is consistent with the treatment of
covariant
, the latter with the treatment offinal
.Thoughts?
cc @munificent @lrhn @eernstg @danrubel @stereotype441 @bwilkerson
The text was updated successfully, but these errors were encountered: