- Notifications
You must be signed in to change notification settings - Fork1.1k
Should we allow nullable boolean ternary operator and let it return null value?#9531
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
bool?maybeCondition=null;int?i=maybeCondition?0:1;// will result as nulllongl=maybeCondition?1:0??-1;// ternary expression would be null so then we can append null coalescing?// if it is impossible because operator precedence we can use parenthesis anywaystrings=(maybeCondition?"yes":"no")??"maybe"; |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 2 comments 4 replies
-
This would be a massive breaking change for existing code. Also there is no syntactic difference making it harder to read. IMHO |
BetaWas this translation helpful?Give feedback.
All reactions
-
How can it be breaking changes? In the current code, ternary operator only work on If this suddenly work it should affect no existing code that can compiled properly, instead it will only affect new code |
BetaWas this translation helpful?Give feedback.
All reactions
-
usingSystem;publicclassC{publicvoidM(){int?maybeCondition=null;varv=maybeCondition>0?1:2;}} This is already legal code. 'v' is an int. It cannot become an |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
@CyrusNajmabadi Oh right, sorry, I forgot Let me change the example code a bit |
BetaWas this translation helpful?Give feedback.
All reactions
-
Using existing mechanisms, these could be written like int?i=maybeConditionswitch{true=>0,false=>1,null=>null};longl=maybeConditionswitch{true=>1,false=>0,null=>-1};strings=maybeConditionswitch{true=>"yes",false=>"no",null=>"maybe"}; |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Yes, I have already write this way at first. And I felt |
BetaWas this translation helpful?Give feedback.