-
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
Compiler can't determine best common type for switch
expression
#8025
Comments
I would also like to highlight another discrepancy related to functions. This does not compile:
However, this compiles:
|
@cston for visibility. I think this is a subtle issue relating to lambda natural type. |
@cston ping for comment on whether or not this is about lambda natural type |
It looks like the best common type calculation for switch expressions currently ignores function types. There is a similar issue with conditional expressions: // error CS0173: Type of conditional expression cannot be determined
var func = (args.Length == 1) ? () => "Hello" : () => "World"; For switch expressions and conditional expressions, we could potentially use function types in the best common type calculation of the overall expression type. |
Moving to csharplang as this is a design question vs. compiler impl. |
Definitely seems odd. I can't come up with a rational as to why array BCT works differently than switch or conditional BCT vis-a-vis function-types. |
IIRC, the team decided against supporting IIFE in those scenarios: |
By explicitly casting the lambdas to Func, you provide the necessary type information for the compiler to determine the return type of the switch expression, and it will compile without errors. |
Probably my understanding of the spec in this part is incorrect, but according to
switch
expression feature spec:And the natural function type has the following:
And:
With following sample which compiles:
So for array construction compiler is able to determine the best common type.
Version Used:
.NET 8.0.101, C# 12
Steps to Reproduce:
Create a
swtich
expression with anonymous lambdas:A minimal repro, with source-code provided, is ideal. Repro @sharplab
Expected Behavior:
Compiles
Actual Behavior:
The text was updated successfully, but these errors were encountered: