question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Proposal] Null-coalescing Assignment Operator: ??=

See original GitHub issue

??= would have semantics analogous to the existing compound assignment operators like +=, %=, and <<=. More precisely, x ??= y would be equivalent to x = x ?? y except that x is evaluated only once. It would have clear, if not vast, utility. Hopefully, it would not be an unreasonably large effort.

It is perhaps debatable whether

object o = GetFirstChoice( foo, bar, someLongArgument )
         ?? GetSecondChoice( foo, bar, someLongArgument )
         ?? GetFallback();

looks better than

object o = GetFirstChoice( foo, bar, someLongArgument );
o ??= GetSecondChoice( foo, bar, someLongArgument );
o ??= GetFallback();

However, the latter seems easier to step through in the debugger.

Furthermore, a use case like

private static void ApplyNewSettings( SettingsSource newSource ) {
    // Set each setting if and only if it is not already set to a non-null value.
    // This could be e.g. applying defaults after deserializing user settings.
    this.SomeSetting ??= newSource.SomeSetting;
    // ...
}

is less repetitive with ??=.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:5
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

9reactions
ashmindcommented, Apr 28, 2016

Given the amount of interest in it (5 linked items) I’ve decided to try making a prototype – which is currently at https://github.com/ashmind/roslyn/tree/features/coalesce-assignment. That’s my first attempt at a Roslyn thing, so I’m sure I’m doing everything wrong.

I’ve also added that branch to TryRoslyn: http://tryroslyn.azurewebsites.net/#b:ashmind-features-coalesce-assignment/K4Zwlgdg5gBAygTxAFwKYFsDcAoADsAIwBswBjGUogQxBBgGEYBvbGNmFAJ0lgA8d2HZN2gwEA9q3b5iZGADcA9mAAmMALIAKAJTMpgtrxgBeGACJeZiQZgB6WzAAWyZLhAAue1DDJHhAHSkiui2KorIEKjItpyKIEQIELZgtMCoILYATAAMAKz6BggwAPzFpvwFAL7YlUA=

7reactions
gaftercommented, Sep 24, 2015

The benefits of this proposal do not overcome the drawbacks in terms of language complexity. It is unlikely to ever have a high enough benefit-for-cost to make us prefer it over many other things we could do in this release or many releases into the future.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Null coalescing assignment - C# 8.0 draft feature ...
This proposal adds a non-overloadable binary operator to the language that performs this function. There have been at least eight separate ...
Read more >
c# - The behavior of null-coalescing assignment operator
C# 8.0 introduces the null-coalescing assignment operator ??=. You can use the ??= operator to assign the value of its right-hand operand to...
Read more >
Proposal: null-coalescing assignment operator ??= #13737
Proposal I'd like to propose an idea for next C#, with name: null-coalescing assignment operator We can combine the ?? and = operators...
Read more >
PHP RFC: Null Coalescing Assignment Operator
??= operator is an assignment operator. If the left parameter is null, assigns the value of the right paramater to the left one....
Read more >
Null coalescing operator
The null coalescing operator is a binary operator that is part of the syntax for a basic conditional expression in several programming languages, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found