-
Notifications
You must be signed in to change notification settings - Fork 205
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
Nullable compound assignments are inconvenient. #1113
Comments
I would say |
@lrhn are you still planning to push on this? |
FWIW I haven't seen much of a need for this in practice yet. |
@Hixie 👍 me neither Have written >100k loc in NNBD and not encountered it. Theory: Examples: for (var i = 0; i < n; i++) var i = n;
while (i > 0) {
i--;
} (same roughly goes for the other compound assignments) |
For ++ specifically I'd be fine with just removing it from the language. :-D |
Haha, off topic but any reason? |
This comment has been minimized.
This comment has been minimized.
I don't think it's worth pushing it right now. We have enough on our plate for the release. It's a feature that can be safely added later since it introduces new syntax which is currently invalid. I think it is worth keeping the issue open in case users end up wanting to use null-aware compound assignments. Then they have a place to report that. |
As I was experimenting NNBD, I ran into a similar issue. I post here a sample code in case it is relevant. I think the lint warning should not happen here. class A {
String? value;
}
class B {
A? a;
}
void main() {
var b = B();
b.a = A();
// Ok
b.a!.value = '123';
// Lint error!
// An expression whose value can be 'null' must be null-checked before it can be dereferenced.
b.a!.value += '456';
} |
@alextekartik The lint is correct because the class member could change from the outside. See dart-lang/sdk#42626. So the flow analysis cannot be sure that |
@creativecreatorormaybenot Ok I understand. I guess it was just puzzling but you are right it is correct. |
I think it might make sense to simplify this issue to "the non-null assertion operator ! should not remove assignability". If that could be achieved it would solve for all the cases It's clear that the syntax is parsing appropriately for this already, since the error on trying to do it that way right now is Note, changing it this way would not introduce any new syntax, it would just make the existing syntax work the way a user would expect. |
(Puts on pedantic hat.) It is technically a change in syntax, which is why the current syntax yells at you. The thing to the left of 1 + 2 = 3;
[4, 5, 6] = 7;
(parentheses) = value;
-negative = positive; Assignment targets are also the things allowed to the left of an increment/decrement operator. The |
Semantic perspective difference I think. From the user perspective the syntax isn't new with that change, it's the existing syntax with different (better) behavior. But fair enough if from the dart developer perspective it's an addition of a different operator. :) |
I've encounter this use case and was wondering why it didn't work:
Just wanted to share :) |
Capturing from a chat discussion, I've seen this come up in a few places now specifically for counting maps, where the pattern is often to initialize everything to zero, then do a lot of |
I've started running into this as well when working with function.inclusiveTicks++; I now need to do: function.inclusiveTicks = function.inclusiveTicks! + 1; Which is significantly more verbose and doesn't feel very Dart-y :-( |
Yes this is ugly. Should be able to do |
Also ought to be able to do // x = x! + 42;
x! += 42;
// x == null ? null : (x = x! + 42);
x? += 42;
// x == null ? null : (x = x! + doSomethingWithSideEffects());
x? += doSomethingWithSideEffects();
// x = y == null || z == null ? null : y! + z!;
x = y? + z?;
// x == null ? null : (x = x! * y + z);
x? = x * y + z; |
With the current Null Safety behavior, a nullable variable like
int? x;
cannot be incremented as easily as a non-nullable variable, even when you know that it currently contains a non-null value.We have
++x
andx += 1
as easy composite operations on non-nullable types, but for the nullable type, when you know it's not null, the only option isx = x! + 1
because we need to insert the!
in the middle of the expression that++x
orx += 1
expands to.It would be nice to have something like
x!+=1
which expands tox = x! + 1
.Draft writeup: https://github.com/dart-lang/language/blob/9a83a94c0278180674dfdd09d97c9eab3a624221/working/1113%20-%20null-asserting%20compound%20assignment/proposal.md
The text was updated successfully, but these errors were encountered: