Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't this C# code compile when using named parameters?

Tags:

c#-4.0

Baffled.

class Test
{
    void Main()
    {
        F(() => "");                // ok
        F(named: () => "");         // 'T' cannot be inferred from the usage!
        F<string>(() => "");        // ok
        F<string>(named: () => ""); // ok
    }

    void F<T>(Func<T> named) { }
}

Could someone tell me why the second call to F fails to compile?

(Note that this is a significantly stripped down example, which is why it seems synthetic. In the real case I came across, there are some default parameters before 'named' and so the named parameter is required. And so, apparently is explicit specification of 'T' by the caller.)

like image 917
scobi Avatar asked Nov 14 '22 14:11

scobi


1 Answers

Seems like an inadequacy in the compiler's delegate type inference...sorry I can't offer more.

like image 152
Jeff Avatar answered Dec 08 '22 00:12

Jeff