-
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
C# nullable types and ternary operator #41883
Comments
This is a duplicate of dotnet/csharplang#2460 |
This is not a duplicate of dotnet/csharplang#2460 since that only considers the target type. This should in fact be a separate issue regarding type determination in the ternary operator. The C# language specification (which is horribly out of date) says:
When one of the result expressions is the dotnet/csharplang#2460 proposes the target type be used. This is fine when that type is known, but not possible when the type is unknown. For instance:
Currently that is not allowed. Instead we have to explicitly type the The proposal would then amount to adding the following to the type resolution rules prior to the failure condition:
That seems reasonably straightforward. In the case of |
This issue is about target typing the ternary operator:
I would suggest that you open a new issue (on dotnet/CSharplang) about your suggestion. |
dup of dotnet/csharplang#33 in which #2460 is mentioned |
@YairHalberstadt For nullable value types specifically target typing is a solution, but it is unnecessary for this issue. A simple addition to the type determination rules for ternary operators handles this case and any other where a value type and an untyped null are used as the potential results. And it doesn't help in the case of Yes, the poster mentions that he supplied a target type, but the simplest resolution here also fixes this in the very common (according to questions on StackOverflow anyway) problem of untyped nulls causing compiler errors. This is an intuitive fix that covers things that target typing explicitly cannot handle and still leaves all of the cases that only target typing can handle alone. It solves the problem of this issue perfectly and simply, and implementation is honestly trivial. Oh, and that trivial, useful fix just got rejected by the design team in favor of a "solution" that doesn't fix most of these problems at all. |
I understand your opinion, but this isn't the right forum for them - CSharplang is where language design discussion goes. It is often the case that the language design team makes decisions that other people don't like. It happens to me all the time. In fact it often happens to members of the team, since they of course often disagree with each other. In such cases it's sometimes possible to change their mind, but you have to do it in a specific way: You have to show them something they didn't know. They already know all the theoretical arguments for one over the other and rejected it. Instead of repeating them, you have to bring data showing that a large percentage of use cases would not be fixed by their solution. For example you could scan dotnet/runtime, find all cases where they have to cast one side of a ternary to get it to compile, and count how many their solution would fix, how many yours would fix, and how many both would fix. I'm other cases you can't change their mind, and that's life. Better luck next time! |
The relevant csharplang issue has, after some discussion, resulted in the feature being sent back to triage for possible inclusion in C# 10. |
Yes, this language question is tracked by dotnet/csharplang#33 already. |
This issue has been moved from a ticket on Developer Community.
Hello.
I'll show on example:
On the left, I explicitly set type is "DateTime?", maybe the compiler could define what is expected from the ternary operator, this would be more obvious and convenient.
Thanks.
Original Comments
Visual Studio Feedback System on 2/14/2020, 03:06 AM:
Thank you for taking the time to provide your suggestion. We will do some preliminary checks to make sure we can proceed further. We'll provide an update once the issue has been triaged by the product team.
The text was updated successfully, but these errors were encountered: