-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Comments
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 |
I think this syntax would be ambiguous with the ternary operator: if you have two 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 I meant something like |
@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 |
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. |
@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 |
and what about the character 'backtick-character' (```)? As far as I know, it does not have a great usage inside C#....
alternatively, I like the syntax of angled brackets, but it would be confusing (because of the indexer- and attribute-syntax):
|
Let's not add syntactic sugar for the sake of adding syntactic sugar. |
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. |
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?
The text was updated successfully, but these errors were encountered: