Project Elvis

Biotronic simen.kjaras at gmail.com
Mon Nov 6 12:25:06 UTC 2017


On Monday, 6 November 2017 at 10:12:11 UTC, Jonathan M Davis 
wrote:
> x ? x : y
>
> and make it
>
> x ?: y
>
> It saves 2 characters plus the length of the variable name. 
> That's it.

I find I often use this in C# with a more complex expression on 
the left-hand side, like a function call. A quick search shows 
more than 2/3 of my uses are function calls or otherwise 
significantly more complex than a variable. Also, it works great 
in conjunction with the null conditional:

foo.Select(a => bar(a, qux)).FirstOrDefault?.Name ?? "not found";

> It seems to be targeted primarily at code that does a lot with 
> classes and is written in such a way that it's not clear 
> whether a class reference should be null or not, whereas most D 
> code doesn't do much with classes.

In my C# code, it's used with strings and Nullable<T> more often 
than with classes.

Given my own experience with the ?? operator, I'd argue it's 
probably not worth it without also including null conditional 
(?.). A quick search in a few projects indicate roughly half the 
uses of ?? also use ?..

--
   Biotronic


More information about the Digitalmars-d mailing list