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

Let conditional operator ?: work on nullable condition #134

Open
Thaina opened this Issue Feb 17, 2017 · 9 comments

Comments

Projects
None yet
6 participants
@Thaina
Copy link

Thaina commented Feb 17, 2017

Sometimes we have bool? condition. Such as dictionary?.TryGetValue(key,out value)

When I want it to return dictionary?.TryGetValue(key,out value) ? value : null it cause error

Maybe we should support ?:?? like this?

return dictionary?.TryGetValue(key,out value) ? value : null ?? null

or maybe just

/// Nullable<bool> ? true : false : null
return dictionary?.TryGetValue(key,out value) ? value : null : null`

@Thaina Thaina changed the title Let conditional operator ?: work on Nullable<bool> Let conditional operator ?: work on nullable condition (or null) Feb 17, 2017

@Thaina Thaina changed the title Let conditional operator ?: work on nullable condition (or null) Let conditional operator ?: work on nullable condition Feb 17, 2017

@YaakovDavis

This comment has been minimized.

Copy link

YaakovDavis commented Feb 17, 2017

Your specific case can be expressed with the following existing syntax (assuming value is of reference type) as:
dictionary?.TryGetValue(key, out value) == true ? value : null

bool? has 3 possible values (true, false & null). Hence supporting it in the ternary operator can lead to unintentional bugs, where the user writes expressions for the true & false cases, but forgets about the existence of the null case. e.g:

//null here means that the operation never started
bool? DidOperationRanToCompletion()
{
   ...
}

bool? result = DidOperationRanToCompletion();
var message = result ? "Success" : "Error"; //clearly this is not what the user intended!
@Thaina

This comment has been minimized.

Copy link
Author

Thaina commented Feb 17, 2017

@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 bool?. It will error unless we add another :

And my example return value : null : null but in the real code we might have difference value for all 3 states so I propose this syntax

@YaakovDavis

This comment has been minimized.

Copy link

YaakovDavis commented Feb 17, 2017

@Thaina

we add another :

OK, got it.

@jnm2

This comment has been minimized.

Copy link
Contributor

jnm2 commented Feb 17, 2017

I agree with @YaakovDavis, dictionary?.TryGetValue(key, out value) == true ? value : null is optimal clarity.
It's no different than what you already have to write in any other condition:

if (dictionary?.TryGetValue(key, out value) == true)
    // ...
@jnm2

This comment has been minimized.

Copy link
Contributor

jnm2 commented Feb 17, 2017

@Thaina what you want is possibly pattern matching for bool?.

@Thaina

This comment has been minimized.

Copy link
Author

Thaina commented Feb 17, 2017

@jnm2 If you read all comment you would see that we already talk about that bool? has 3 states

And while we could use pattern matching I think 3 states is too common so we should just support it with ternary operator

@jnm2

This comment has been minimized.

Copy link
Contributor

jnm2 commented Feb 17, 2017

If you read all comment you would see that we already talk about that bool? has 3 states

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.)

@initram

This comment has been minimized.

Copy link

initram commented Apr 3, 2017

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.

@MgSam

This comment has been minimized.

Copy link

MgSam commented Apr 3, 2017

  • C# was specifically designed so that null != false. This is a great feature and prevents accidental bugs.
  • You haven't presented any positives for your proposed quaternary operator. Your own example has you returning he same value, null, in two of the cases. How is that an improvement? It's both more verbose and less clear.

Any feature added should improve the language for everyone and be worth the high implementation cost to the language designers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment