-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Proposal: conditional with value type and null. #438
Comments
Yes! 👍^💯 |
👍 |
Hell yeah! This has already been a major annoyance to me. |
+1 that is very irritating. |
Yes, that syntactic sugar would be very nice. |
supercat explained the potential problems with that -- still, it might be worth considering to allow it whenever the types could be determined unambiguously... |
@axel-habermaier I believe this request is simply to special case a struct value of type |
@axel-habermaier, there should be no problems with value types. It is interesting that in the following case it actually finds "implicit conversion between 'int' and int val = 5;
if (val == null) |
@omariom , It's a conversion but not in the way you think. The C# compiler is looking for an This too is also legal, |
@mburbea That's not what happens with C# specification, §7.3.7 Lifted operators
A similar mechanism could be introduced for |
@MrJul, 👍 |
Err no. @MrJul that is not correct.
You can get your desired behavior of lifted operators, by declaring val as a int? or by specifying that int? val = 3;
if(val == null) // works! no boxing.
//or
int val = 3;
if(val == (int?)null) // again no boxing. |
No such IL will ever be generated since the compiler is smart enough to see the branch is always false. Still, I disagree with you, it's the lifted Anyways, whether its boxing or lifting, our argument doesn't add much to the original proposal. I was simply suggesting to use a similar behavior as lifted operators. I propose to amend the spec (§7.14 conditional operator). In bold:
|
@MrJul in your spec we could even replace "literal" with "constant value null". That way this would work with more esoteric code like |
I think using the conditional operator in conjunction with structs can be unambiguously defined. C# is already aware of nullable values: it even has a special syntax, with the question mark. You should be able to compile this code:
to roughly the same cil that this code compiles to:
|
I'm going to largely copy my comment from #11886 because it shows one of the surprises that this results in, i.e. that the following is legal:
but this is not:
As programmers learn C# they go for the ternary operators an awful lot and it's pretty difficult to try and explain why the first example works and the other doesn't. |
We are now taking language feature discussion in other repositories:
Features that are under active design or development, or which are "championed" by someone on the language design team, have already been moved either as issues or as checked-in design documents. For example, the proposal in this repo "Proposal: Partial interface implementation a.k.a. Traits" (issue 16139 and a few other issues that request the same thing) are now tracked by the language team at issue 52 in https://github.com/dotnet/csharplang/issues, and there is a draft spec at https://github.com/dotnet/csharplang/blob/master/proposals/default-interface-methods.md and further discussion at issue 288 in https://github.com/dotnet/csharplang/issues. Prototyping of the compiler portion of language features is still tracked here; see, for example, https://github.com/dotnet/roslyn/tree/features/DefaultInterfaceImplementation and issue 17952. In order to facilitate that transition, we have started closing language design discussions from the roslyn repo with a note briefly explaining why. When we are aware of an existing discussion for the feature already in the new repo, we are adding a link to that. But we're not adding new issues to the new repos for existing discussions in this repo that the language design team does not currently envision taking on. Our intent is to eventually close the language design issues in the Roslyn repo and encourage discussion in one of the new repos instead. Our intent is not to shut down discussion on language design - you can still continue discussion on the closed issues if you want - but rather we would like to encourage people to move discussion to where we are more likely to be paying attention (the new repo), or to abandon discussions that are no longer of interest to you. If you happen to notice that one of the closed issues has a relevant issue in the new repo, and we have not added a link to the new issue, we would appreciate you providing a link from the old to the new discussion. That way people who are still interested in the discussion can start paying attention to the new issue. Also, we'd welcome any ideas you might have on how we could better manage the transition. Comments and discussion about closing and/or moving issues should be directed to #18002. Comments and discussion about this issue can take place here or on an issue in the relevant repo. This proposal is being tracked at dotnet/csharplang#33 |
Currently (at least pre-roslyn), the code
int? x = a ? 1 : null
gives the errorand instead you have to type
int? x = a ? 1 : (int?)null
.Can this be changed in a future version?
The text was updated successfully, but these errors were encountered: