Open gurustron opened 9 months ago
I would also like to highlight another discrepancy related to functions.
This does not compile:
var str = (() => "Hello World")(); // does not compile
However, this compiles:
var str = ((Func<string>)(() => "Hello World"))(); // 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.
I would also like to highlight another discrepancy related to functions.
IIRC, the team decided against supporting IIFE in those scenarios: https://github.com/dotnet/csharplang/blob/main/meetings/2021/LDM-2021-07-26.md#direct-invocation-of-lambdas
By explicitly casting the lambdas to Func
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: