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

Proposal: Add 'is not null' operator #1944

Closed
lellid opened this issue Oct 19, 2018 · 17 comments
Closed

Proposal: Add 'is not null' operator #1944

lellid opened this issue Oct 19, 2018 · 17 comments

Comments

@lellid
Copy link

lellid commented Oct 19, 2018

Currently, we already have something like

if(a is null)
  …

The opposite case, the question if something is not null, is (asymmectrically) much harder to formulate. It would be nice to have something like

if(b is not null)

or ideally

if(b is not null bnn)

so that bnn is then guaranteed to be not null in a thread safe way, would make life much easier.

@CyrusNajmabadi
Copy link
Member

if(b is not null bnn) will be able to be written as if (b is {} bnn) once 'property patterns' go in.

@jaredpar
Copy link
Member

Pretty sure is not null falls out of pattern matching work. @gafter

@ufcpp
Copy link

ufcpp commented Oct 20, 2018

#27
#157
#246
#568
#1350
#1913

@Austin-bryan
Copy link

What's wrong with if (!b);

@markusschaber
Copy link

@willard720 if (!b)already has defined semantics if b is bool or bool?, and AFAICS every type which has an implicit conversion to bool.

@gafter
Copy link
Member

gafter commented Oct 22, 2018

Pretty sure is not null falls out of pattern matching work.

Not until we do #1350 which is not currently expected to hit C# 8.0.

@Austin-bryan
Copy link

@markusschaber True, but most people don't do an implicit conversion to bool, and I can't imagine what else would make sense for it. If someone did the code if (employee) instead of if (employee.IsPunchedIn) then they're using bad practices, so if (employee) will almost always mean not null.

@markusschaber
Copy link

@willard720 It just might be confusing, and it might not be easy to resolve the conflicts with existing semantics ambigously.

e. G. for bool?, will it mean not null or equal to true?

Other languages have rather intuitive concepts of "truth value" (like Python, where thinks like 0, 0.0, None and empty collections or strings evaluate to false, while their non-emtpy counterparts evaluate to true). It feels natural, and I actually like it, but I'm not sure whether it's possible to introduce such a concept in C# now, without breaking existing code.

And I doubt that being null alone is the best criteria for "truth values", I'd rather see some concept like in Python.

However, I'm afraid we're off-topic for this issue. :-(

@MkazemAkhgary
Copy link

MkazemAkhgary commented Oct 29, 2018

if(a is not null)
if(a != null)

This ancient syntax is still better. for programmers its easy to catch.

And what is bnn for? a is already guaranteed to be not null inside that block. I dont get your thread safety argument.

@CyrusNajmabadi
Copy link
Member

CyrusNajmabadi commented Oct 29, 2018

And what is bnn for? a is already guaranteed to be not null inside that block. I dont get your thread safety argument.

If 'a' is not a local/parameter, there is no guarantee that it will be non-null, even after checking it against null. Specifically, another thread could end up changing it's value. 'Patterns', on the other hand, allow you to perform tests, then place the results in a local when successful. Thus giving you certainty about the state of the variable if the pattern succeeds in matching.

@MkazemAkhgary
Copy link

@CyrusNajmabadi Im illuminated! 🌝

@RichardD2
Copy link

@willard720 if (!b)already has defined semantics if b is bool or bool?, and AFAICS every type which has an implicit conversion to bool.

Also for types which define the true and false operators.

@markusschaber
Copy link

@RichardD2 I did not even know about those operators....
I'm doing C# for 10 years now, and I still learn something new every day... :-)

@gafter
Copy link
Member

gafter commented Nov 9, 2018

As @CyrusNajmabadi pointed out, this will be available in C# 8.0 though not with precisely the syntax you suggest. Supporting not patterns is something we are looking at post C# 8.0.

@RichardD2
Copy link

Surely if (b is not null) ... can already be written in C# 7 as if (b is object) ...? A null reference will only match the null or var patterns.

The compiler even seems to correctly handle value types (generates a CS0183 warning, and omits the test from IL) and nullable value types (replaces the test with a call to b.HasValue).

Of course, it wouldn't work for b is not null bnn, since you'd presumably want bnn to have the same compile-time type as b, whereas with b is object bnn, it would be an object.

@Joe4evr
Copy link
Contributor

Joe4evr commented Nov 14, 2018

Of course, it wouldn't work for b is not null bnn, since you'd presumably want bnn to have the same compile-time type as b, whereas with b is object bnn, it would be an object.

See Cyrus' earlier comment.

@YairHalberstadt
Copy link
Contributor

Closing as not patterns have been implemented for C# 9.0 as part of #1350

Whilst if(b is not null bnn) is not currently allowed, you can do if(b is {} bnn) with the equivalent meaning, and if the championed #3369 is implemented is not null bnn would have a different meaning.

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

No branches or pull requests