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

Null check syntactic sugar; Make foo? equivalent to foo != null #833

Closed
kingces95 opened this issue Aug 19, 2017 · 5 comments
Closed

Null check syntactic sugar; Make foo? equivalent to foo != null #833

kingces95 opened this issue Aug 19, 2017 · 5 comments

Comments

@kingces95
Copy link

So here's my weekend idea...

Null checks are common so why not sprinkle on some (more) syntactic sugar?

Instead of

if (myArg == null) throw new ArgumentNullException()

how about

if (!myArg?) throw new ArgumentNullException()

We've already become accustom to an expression that starts with myArg? being declarative syntax for a null check followed by property access (as in myArg?.Prop) or a null check followed by an alternate expression (as in myArg ?? myOtherArg). So if myArg? is followed by nothing, then... well... it's just a null check resulting in a bool (which could possibly be followed by some procedural action like throwing an exception).

@HaloFour
Copy link
Contributor

If obj? produced a bool it would be expected that obj?.ToString() would return either "True" or "False", but that is already legal syntax that has a completely different behavior. This proposal would either break this behavior or introduce ambiguities.

@MillKaDe
Copy link

.. has already been suggested a few times .. See:

Possible syntax (unary prefix operator with highest precedence):

C# (now) C# (proposed) C/C++ remarks
if (p != null) .. if (?p) .. if (p) .. = !ReferenceEquals (null, p)
if (p == null) .. if (!?p) .. if (!p) .. = ReferenceEquals (null, p)

The ! operator for bool already exists, and the ? operator for non-null checks would be implemented something like this ..

static public bool operator ? (T t) where T : class => !ReferenceEquals (null, t);

For details about this particular syntax see the two linked threads.

Both threads mentioned above are closed right now, but I still think a null check operator would be really nice.

@MillKaDe
Copy link

@HaloFour : I think a prefix instead of postfix operator would avoid the problems you mention.

@HaloFour
Copy link
Contributor

My opinion is that an equality comparison is succinct enough and significantly more readable than either operator-based proposal.

@kingces95
Copy link
Author

@HaloFour, ah, yeah. Foo?.ToString() is ambiguous. Duh. Well, that's why language design should be left to the professionals!

@MillKaDe thanks for the links. The prefix ops may work but I gotta say I'm more accustom to seeing the question mark as a postfix operator..

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

3 participants