Roslyn: Conditional coalescing operator

Created on 28 Jan 2017  路  9Comments  路  Source: dotnet/roslyn

I would like to suggest an extension of the conditional operator:

var boolean1 = true;
var boolean2 = false;

vat text = "text";

text = boolean2 ? "newtext";
// text

text = boolean1 ? "newtext";
// newtext

This should simplify the current alternatives:

text = boolean1 ? "newtext" : text

if(boolean1) text = "newtext"

I appreciate there might be difficulties with an operator that can potentially return nothing in one case, but its usage could be restricted only to assignment.

Area-Language Design Discussion

Most helpful comment

I think it's always useful to reduce verbosity

Often it's not, especially when the purpose of the expression is unclear. The behavior of your syntax is very unclear. It looks like an expression, which implies that it can be used anywhere that an expression can be used, but that is certainly not the case. Expressions and assignments are two different things in C#.

Your proposed syntax is also ambiguous:

var foo = boolean1 ? boolean2 ? "foo" : "bar";

Your proposal would make the above legal, but it would be impossible for the compiler to tell what it should assign to foo, if anything.

All 9 comments

I don't like the idea of an operator that converts an expression into a statement. It severely limits the potential usage of such operator to the point where it doesn't seem to be worth it. Is it really that much of a verbosity burden to manually write either an if statement or a ternary conditional expression? You're not saving more than two characters vs. the statement form.

Doing the test and assignment in the declaration solves this.
```cs
var text = boolean1 ? "newtext" : "text";
````
It's unclear from your example therefore, what benefit this would bring to the language.

@DavidArno void Method(ref string text, bool boolean) => text = boolean ? "newtext"

@StefDotmailer,

Would would be the point of that as the modification of the text parameter would be lost?

@DavidArno I was hoping I didn't have to write more code to make the concept understandable:

string Text { get; set; }
void Method(bool boolean) { Text = boolean ? "newtext"; }

I added a ref to the other example to make you happy :)

@StefDotmailer,

But you are simply saving three characters, compared with:
cs string Text { get; set; } void Method(bool boolean) { if (boolean) Text = "newtext"; }

Is it really worth a whole new language feature just to save three characters?

@DavidArno I think it's always useful to reduce verbosity, if it becomes just syntactic sugar for the if, I don't even think is too much effort.

But it's up for discussion so we can see what others think, too.

I think it's always useful to reduce verbosity

Often it's not, especially when the purpose of the expression is unclear. The behavior of your syntax is very unclear. It looks like an expression, which implies that it can be used anywhere that an expression can be used, but that is certainly not the case. Expressions and assignments are two different things in C#.

Your proposed syntax is also ambiguous:

var foo = boolean1 ? boolean2 ? "foo" : "bar";

Your proposal would make the above legal, but it would be impossible for the compiler to tell what it should assign to foo, if anything.

@HaloFour good point about the ambiguity. Maybe a different syntax (I.E. another symbol) can be used also not to confuse it with an expression.

Was this page helpful?
0 / 5 - 0 ratings