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

Idea: Simplify null check syntax #9606

Closed
christianarg opened this issue Mar 9, 2016 · 11 comments
Closed

Idea: Simplify null check syntax #9606

christianarg opened this issue Mar 9, 2016 · 11 comments

Comments

@christianarg
Copy link

Context
C#6 incorporated the very handy ?. operator. This way I can change this

if(someClass != null && someClass.someField == "someValue) { .. }

into this:

if(someClass?.someField == "someValue) { .. }

That's pretty nice.

Idea

When I have to check if a variable is null, I have to do this:

if(someVariable == null) { .. }

Wouldn't it be nice if we could do something like this:

if(someValue?) { .. }

The counter part is, if I want to check if a variable is NOT null, what would be do? this?:

if(!someValue?) { .. }

Kinda ugly.

What about this?:

if(someValue!?) { .. }

Bit weird. Probably something better but you get the idea.

Thoughts? Handy or perhaps too much syntax sugar?

@HaloFour
Copy link

HaloFour commented Mar 9, 2016

You'll get this with pattern matching:

if (someValue is SomeClass { SomeField is "SomeValue" }) { ... }

Are you otherwise proposing a new Boolean operator to detect whether or not a value is null?

@svick
Copy link
Contributor

svick commented Mar 9, 2016

I think this syntax would be ambiguous with the ternary operator: if you have two ? before a single :, how is the compiler (and the programmer) supposed to figure out which one is for the ternary operator and which one is for your proposed null check operator?

I'm unable to come up with a reasonably simple compiling example. But my problem is mostly to get the types right and the parser doesn't take types into account, so I think it's still an issue.

@alrz
Copy link
Member

alrz commented Mar 9, 2016

e??e:e; is not ambiguous but needs look ahead to figure out it's not a null coalescing operator.
e?.M(); is ambiguous, however, the compiler could parse it as a null conditional when you dot off a "null check expression".

@svick
Copy link
Contributor

svick commented Mar 9, 2016

@alrz I meant something like a? +a? +a : a, which could be either interpreted as (a != null) + a ? (+a) : a or as a ? ((+a) != null) + a : a (both interpretations can actually compile, if the type of a is crazy enough to overload the necessary operators).

@alrz
Copy link
Member

alrz commented Mar 10, 2016

@svick I see. If it was a prefix operator, then it woudn't be ambiguous in this example at least,

?a ?+a +a : a
(a != null) ? +a+a : a

But still parsing is a little tricky. Also, just like throw expression, it could be restricted to some specific contexts such as first operand of the ternary operator, conditional of the if statement and so on.

@HaloFour
Copy link

Outside of the simplest of cases this "operator" produces some really confusing looking code. I don't think it's worth it just to avoid typing a few extra characters. Null propagation and pattern matching should eliminate most of the "need" of something like this anyway.

@alrz
Copy link
Member

alrz commented Mar 10, 2016

Outside of the simplest of cases

@HaloFour That was my point, if the context is restricted then it woudn't be possible to use it outside of the simplest of cases, just directly inside if and other _boolean-expression_s.

@Unknown6656
Copy link

and what about the character 'backtick-character' (```)? As far as I know, it does not have a great usage inside C#....
One could use it like this:

int? a = null;
if (`a) ....   // true
int b = `a ? 1 : 2;

alternatively, I like the syntax of angled brackets, but it would be confusing (because of the indexer- and attribute-syntax):

int? a = null;
if ([a]) .... // true
int b = [a] ? 1 : 2;

@Miista
Copy link

Miista commented Mar 13, 2016

Let's not add syntactic sugar for the sake of adding syntactic sugar.

@gafter
Copy link
Member

gafter commented Mar 20, 2017

We are now taking language feature discussion on https://github.com/dotnet/csharplang for C# specific issues, https://github.com/dotnet/vblang for VB-specific features, and https://github.com/dotnet/csharplang for features that affect both languages.

@gafter gafter closed this as completed Mar 20, 2017
@MillKaDe
Copy link

There are two similar proposals / discussions:

A newer one in C#Lang : #196 : Discussion: null evaluation operator to boolean

An older one in Roslyn : #11577 : [Proposal] Simplify boolean test of null with objects

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants