Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Should we allow nullable boolean ternary operator and let it return null value?#9531

Unanswered
Thaina asked this question inLanguage Ideas
Discussion options

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";
You must be logged in to vote

Replies: 2 comments 4 replies

Comment options

This would be a massive breaking change for existing code. Also there is no syntactic difference making it harder to read. IMHO

You must be logged in to vote
3 replies
@Thaina
Comment options

How can it be breaking changes?

In the current code, ternary operator only work onbool expression and error when it used on expression that result onbool?, so there should be nowhere using it on any expression that not return exact true or false

If this suddenly work it should affect no existing code that can compiled properly, instead it will only affect new code

@CyrusNajmabadi
Comment options

usingSystem;publicclassC{publicvoidM(){int?maybeCondition=null;varv=maybeCondition>0?1:2;}}

This is already legal code. 'v' is an int. It cannot become anint? (or null) just becausemaybeCondition is null.

@Thaina
Comment options

@CyrusNajmabadi Oh right, sorry, I forgot((int?)null) > 0 is not result in nullable, it just normal bool and false, and I don't intend to change that, it was actually sample code unrelated to my proposal

Let me change the example code a bit

Comment options

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"};
You must be logged in to vote
1 reply
@Thaina
Comment options

Yes, I have already write this way at first. And I felttruefalsenull and=> is unnecessary annoying and distracted when it always have just these 3 states

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Labels
None yet
4 participants
@Thaina@siegfriedpammer@CyrusNajmabadi@jilles-sg

[8]ページ先頭

©2009-2025 Movatter.jp