-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Q: Why not allowing null
instead of false
in conditionals?
#438
Comments
It would be a breaking change if |
I don't think so. At least in the case of |
Well said. |
@jnm2 |
void Foo(Bar bar, bool option) { }
int? x = null;
Foo(bar, x > 1); // Suddenly fails to compile because no overload of Foo takes (Bar, bool?).
var arg1 = new object();
var arg2 = new object();
if (arg1 = arg2) // Oops, this is now valid C#
{
// ...
} More specifically, "false" and "lack of information" are not the same thing. "No" and "N/A" should never be conflated. https://stackoverflow.com/questions/3197132/why-doesnt-null-evaluate-to-false |
@jnm2
But in all other cases, the outcome of the conditional expression being |
Another example where this would be an issue is
If code like |
We can't have the insides of expressions be evaluated differently depending on an outer container. If the expression returns |
I think I have seen in the code that the evaluation of the conditional statements is done after the inner expression has been lowered. So, a "boolean |
@svick
So, the question still stands. Where would this really matter ? (Except from the assignment problem) |
@lachbaer The difference is, when I write With your proposal, that code would just compile, I would not think about the third case, and so there is a good chance the code I just wrote would be buggy. No pit of success for me. |
To quote Eric Lippert:
|
I like the idea of making null checks shorter, because they are all over my code. But (!obj) might conflict with possible future notations for non-nullability for reference types. Many would like to see '!' as a symbol for that a reference type variable cannot be null, for example:
Where it means less lenience towards null, while in the proposed notation
it would mean more lenience towars null, which could become confusing. Maybe this would be better a better alternative:
That notation is more like other null-conditional notations. |
I recently figured that out 😉
That makes sense and it wasn't that obvious to me. Thank you all for clearing that up! 👍 @janjoostvanzon
That was the main reason for this question, just first I couldn't see why Now I have a further idea: The questionmark-style would be absolutely intuitive and fit in the current use of it. if? (obj) { /*...*/ }
if? (list?[index].item) { /*...*/ }
if? (checkbox.IsChecked) { /*...*/ } // Only interested in IsChecked == true
if? (objA = objB) { /*...*/ } // put the ? there intentionally
if? (objA == objB) { /*...*/ } // Warning: condition can never be null
// equals in meaning
if (obj != null) { /*...*/ }
if (list?[index].item != null) { /*...*/ }
if (checkbox.IsChecked.GetValueOrDefault()) { /*...*/ }
if ((objA = objB) != null) { /*...*/ } (the condition must be either of type And maybe foreach? (var item in list) { /*...*/ }
// equals in meaning
if (list != null) {
foreach (var item in list) { /*...*/ }
} These are probably realistic use cases. What do you think? |
I like your enthusiasm with my reaction. I felt that my proposal would take away the awkward ambiguity between bool? being null or false, which definitely matters. Now for the alternative notation you proposed:
I feel that my proposal would work better:
because you could be more specific about exactly which variable will cause the 'if' to be skipped when it is null. For instance:
In your proposal both the programmer and the computer should simply 'guess' for which variable null matters? That will create ambiguity readability wise, and a performance impact computer-wise. Some people need to squeeze the max of performance out of their C# code and then the amount of guesses and checks the computer has to do matters, especially when things are called repeatedly. My proposal would also work with other keywords, like 'while' and 'foreach'. I do see a problem with for instance indexers and property paths and the notation I proposed:
There you cannot see, if the above means 'return null if list is null' or: 'skip the if block if list is null'. The latter might be expressed as:
I do not know how the designers of the C# language themselves would feel about that.
I also wonder how the other partipants in this conversation feel about this. |
I pondered over your propsal for 'return?' and 'yield return?' #437 (return only if expression is not null), where the ? being near the keywords does seem to work better. Perhaps 'if?' could simply mean skip the if, if and only if the entire condition expression is null. I am going back and forth about this proposal of yours. Perhaps both notations could work:
But the question mark at the end of a variable would allow you to be more specific. |
Don't forget user-defined implicit conversions to boolean, that potentially breaks many of the above mentioned assumptions. |
Meanwhile, after being more sensibilized for this issue, I must admit that I have only seen a few cases where Regarding the opposite construct of |
The inital question has been comprehensively answered. Thank you all! 😊 |
@lachbaer You're a sport. Much respect. |
Question
Conditionals in
if
,while
,when
& co must be type safeboolean
expressions - for an initially good reason. (How many errors aways occur in C/C++ because of being not like that?!)But there are so many cases where there are expressions like
obj != null
andobj == null
.So, what actually speaks against evaluating any non-null object or nullable to a boolean
true
, in case of conditionals, that being constructs, where the conditional is wrapped in parantheses (()
).Examples
(The examples show the syntax and semantics)
But the following must not work
because, the
!
operator works onobj
. That obviously doesn't work unless no implicitboolean
conversion exists.In case
obj
had atrue
operator (or bool conversion), that one will be respected, unlessobj
is null, what would lead to afalse
condition anyhow.would be evaluated to
null
iflist
isnull
. Here, also, thisnull
could be safely evaluated to false and would still be meaningful enough.Back to the question
Besides the whole idea of type safe boolean conditions, isn't it also safe enough to always respect
null
as also beingfalse
?The text was updated successfully, but these errors were encountered: