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

Operator =?? (opposite to ??=) #288

Open
rostopira opened this issue Mar 23, 2019 · 2 comments
Open

Operator =?? (opposite to ??=) #288

rostopira opened this issue Mar 23, 2019 · 2 comments
Labels
feature Proposed language feature that solves one or more problems null-aware-expressions Issues proposing new expressions to manage null

Comments

@rostopira
Copy link

rostopira commented Mar 23, 2019

I propose to make =?? operator
Expression a =?? b works as

if (b != null)
  a = b;

If ??= exists, why this one doesn't?

@lrhn lrhn added the feature Proposed language feature that solves one or more problems label Mar 26, 2019
@ThinkDigitalSoftware
Copy link

This can be accomplished by doing

a = b ?? other;

This is equivalent to

if(b != null)
    a = b;
else
    a = other;

This seems to work in any case I can think of. If you don't want to change a if b is null,
then use

a = b ?? a;

@lrhn
Copy link
Member

lrhn commented Jul 5, 2019

The general problem is that of if (x != null) .... some operation containing x .....
That syntax works, but can feel cumbersome. In particular it often requires an extra variable because you need to both test and use the value of an expression.

We have added some extra syntax in some cases, where you can test a value and do something in a single expression. Those are (currently):

  • e1 ?. selectorslet tmp = e1 in tmp == null ? null : tmp.selectors
  • e1 ?? e2let tmp = e1 in tmp != null ? tmp : e2
  • x ??= e2let tmp = x in tmp != null ? tmp : x = e2

These are all defined to work on a very specific syntaxes, so it's clear to the user what the scope of the operation is.

I'd prefer to not add more specific operators, unless they are very generally useful (which the "specific" likely prevents), so I'd defer to #219.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems null-aware-expressions Issues proposing new expressions to manage null
Projects
None yet
Development

No branches or pull requests

4 participants