Replies: 9 comments
-
Your specific case can be expressed with the following existing syntax (assuming
|
Beta Was this translation helpful? Give feedback.
-
@YaakovDavis That's why I was suggest /// Nullable<bool> ? true : false : null
return dictionary?.TryGetValue(key,out value) ? value : null : null` I mean we cannot use the normal ternary operator directly on And my example return |
Beta Was this translation helpful? Give feedback.
-
OK, got it. |
Beta Was this translation helpful? Give feedback.
-
I agree with @YaakovDavis, if (dictionary?.TryGetValue(key, out value) == true)
// ... |
Beta Was this translation helpful? Give feedback.
-
@Thaina what you want is possibly pattern matching for |
Beta Was this translation helpful? Give feedback.
-
@jnm2 If you read all comment you would see that we already talk about that And while we could use pattern matching I think 3 states is too common so we should just support it with ternary operator |
Beta Was this translation helpful? Give feedback.
-
Yes, I did. The three states is what suggested pattern matching to me because > 2 states is where pattern matching shines. I also read the comments describing the problems with your suggested quaternary operator and I agree with them. (Aside. I want to nip this in the bud. "If you read all comment you would see" -> this is objectively rude. I am not upset and I am happy to overlook this rudeness and continue the conversation. On the other hand, I've seen this same path to rudeness and negativity drag down other threads at /roslyn with other people. I know you know what I'm talking about. I don't want the same thing to happen here. I would prefer that we all speak in ways that give each other the benefit of the doubt in all cases. We all make mistakes and should treat one another with respect.) |
Beta Was this translation helpful? Give feedback.
-
I would also like be able to use nullable bools in "if", "where" and "for" conditions. When you can write if(obj.field.isGood) then it also feels normal that you can write if(obj?.field.isGood) This makes the refactoring the above to include null checks cleaner and easier. For me it always feels weird to have to compare to true in a condition as it screams redundancy at me. For me there is no interest in distinguishing false and null in these cases. And if that was really needed then you could always be more explicit to achieve that. |
Beta Was this translation helpful? Give feedback.
-
Any feature added should improve the language for everyone and be worth the high implementation cost to the language designers. |
Beta Was this translation helpful? Give feedback.
-
Sometimes we have
bool?
condition. Such asdictionary?.TryGetValue(key,out value)
When I want it to
return dictionary?.TryGetValue(key,out value) ? value : null
it cause errorMaybe we should support
?:??
like this?return dictionary?.TryGetValue(key,out value) ? value : null ?? null
or maybe just
Beta Was this translation helpful? Give feedback.
All reactions