Skip to content
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

New null operator =?? #1632

Closed
flywheelms opened this issue May 14, 2021 · 5 comments
Closed

New null operator =?? #1632

flywheelms opened this issue May 14, 2021 · 5 comments
Labels
state-duplicate This issue or pull request already exists

Comments

@flywheelms
Copy link

This tracker is for a null safety language suggestion.

Just as today we have ??= to make an assignment if the left operand is null, it would be highly useful to have a corollary operator.

=?? would not make an assignment if the right operand is null

theVariable =?? someNullValue;
... would be less verbose with clearer intention than...
theVariable = someNullValue ?? theVariable;

@eernstg eernstg transferred this issue from dart-lang/sdk May 15, 2021
@eernstg
Copy link
Member

eernstg commented May 17, 2021

The intuition would be "compute a value; if it is non-null then store it, otherwise do nothing". This is a quite meaningful action, so it would be nice if we could express it without repeating the variable name (and without having that no-op assignment).

=?? breaks the current pattern where variants of assignment are always of the form ...= rather than =..., but it does not seem to create any parsing problems (basically, no expression can start with ??, so we won't confuse v =?? e with v = (?? e)). So it's quite promising!

We should note the connection to other proposals about going ahead / skipping expression evaluation in cases where a given value is null.

For instance, e1?.m(e2) will skip evaluation of e2 and the invocation of m (plus possibly some subsequent member access operations, based on null shorting) whenever e1 evaluates to null, but we don't have a way to specify that the invocation should be skipped if e2 evaluates to null. We've had several sketches of proposals (e1.m(??e2) and such). So if we could write v = e as v.assign(e) then we could use v.assign(??e) to get the semantics proposed here, and this could then be allowed to take the form v =?? e;. ;-)

@Levi-Lesches
Copy link

I believe this is a duplicate of #288. A collection of more null-aware syntax by @lrhn is here.

I can understand updating old variables, but how would this work with declaring new ones?

int one = getNonNull();
one =?? nullable();  // skipped
int two =?? nullable();  // what happens here?
int? three =?? nullable();  // or here?

@lrhn
Copy link
Member

lrhn commented May 25, 2021

The syntax doesn't need to work for declarations. You can't write var x ??= 42; either. Initialization is not the same as assignment, even though the syntax is similar.

@lrhn
Copy link
Member

lrhn commented Jun 16, 2021

If we had a pipe operator, say ->, which should also exist as null aware, and setter-tear-offs, x=, then x =?? e; could be written as e ?-> x=;.
(Which looks mostly like random line noise.)

Or if we had unary function shorthands, e ?->=>x=it;.

We should totally have a pipe operator!

@lrhn
Copy link
Member

lrhn commented Jun 16, 2021

(But this is a duplicate of #288, so closing in favor of that).

@lrhn lrhn closed this as completed Jun 16, 2021
@lrhn lrhn added the state-duplicate This issue or pull request already exists label Jun 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state-duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

4 participants