-
Notifications
You must be signed in to change notification settings - Fork 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
Discussion: consider target-type when the compiler can't determine the type of a ternary #877
Comments
This way, the behaviour will be inconsistent, as the target type is available for assignment LHS but not for arbitrary expressions.
would work whereas
would not. |
@vladd, string s = ((c ? a : b) as C1).ToString(); would work. That would overcome any inconsistency. |
@DavidArno Note: |
@vladd The ternary in |
@alrz, nor does |
@alrz If I understand correctly, in C# pretty much every expression (with exception of method groups/lambdas and The way you are proposing breaks this logic, as the conditional expression would need to know the outer type in order to infer the inner types. This raises the question: where should be the border between top-to-bottom and bottom-to-top type inference? |
Target-typing is a well-defined concept in the language. ternary itself does apply it to either of operands that does not have a type, e.g. |
@alrz I see your point. Till now typeless (or top-to-bottom-typed) expressions were either simple expressions like |
EDIT: that example just works with dotnet/roslyn#22162 in place. so not related here. |
Does that imply that the language team is also relaxing the restrictions on the use of |
@HaloFour Relevant PR: dotnet/roslyn#22162, being used over here: dotnet/corefxlab#1742 |
Closing in favor of championed issue #2460 |
Currently, C# requires either of operands in a ternary to be convertible to one another, e.g.
But in the above example the compiler could consider the target-type
C1
for the conditional expression instead of requiring a cast on either of operands.Note that the proposal #33 has been limited to nullables so that's not an alternative here.
Other constructs that can be target-typed:
??
operator:SyntaxNode x = expression ?? statement;
.switch
expressions:Size size = e switch { p1 => new(1, 2), p2 => new(3, 4) };
The text was updated successfully, but these errors were encountered: