Replies: 8 comments 8 replies
-
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
if (a?.TryGetValue("h", out var _) ?? false)
Console.WriteLine("Dictionary not null and found value found");
if (a?.TryGetValue("h", out var _) ?? true)
Console.WriteLine("Dictionary null or found value found"); |
Beta Was this translation helpful? Give feedback.
-
We definitely cannot do this. This is a normal C# pattern and tehre is absolutely code out there that depends on the tri-state nullable-boolean semantics that the language has here (and has had since C# 2.0). |
Beta Was this translation helpful? Give feedback.
-
I don't think this is true. In many C derived languages, there's no actual bool type - just a convention that Some of these literally use a preprocessor to do the "heavy lifting".
In those languages, the comparison It's also worth noting that practically the only places I ever write |
Beta Was this translation helpful? Give feedback.
-
By "normal interpretation" you really mean intuition, not what you might expect but there is only a single and objective interpretation here that's defined by the language. |
Beta Was this translation helpful? Give feedback.
-
anyone who has delt with sql has the intuition that you have to be careful with null - this is how sql would interpret these (in)equalities: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I've recently noticed that comparing for == false and != true doesn't mean the same when using the ? accessor for possibly null objects.
Given the following example:
The normal interpretation would be that 1 and 2 should be interchangeable, as in one case we are checking for !true and the other for false, which mean the same.
However, the code gets translated like this:
From a simple glance, 1 and 2 are actually very different. 1 is checking for either a is null or !a.TryGetValue, whereas 2 is checking for a is not null and !a.TryGetValue.
It is not obvious at all that changing the comparison from != true to == false would cause this, as in general they would be expected to mean the same.
So, a?.TryGetValue("h", out var _) == false should be true if a is null (TryGetValue should be false in this case by default, as there is no dictionary, which means no result, ergo false), or a is not null and !a.TryGetValue is true, like in scenario 1.
Please make the behavior consistent so that != true and == false generate the same code, and generate the code in scenario 1.
This is a not obvious foot gun which causes us to not use ? for checking negatives, as it's not expected that == false and != true behave differently in general, so we instead manually write the code for scenario 1.
Beta Was this translation helpful? Give feedback.
All reactions