Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to infer generic type with optional parameters

Given the following method signature, why is it when a parameter is explicitly named the compiler is unable to automatically infer the type? Visual Studio 2010 SP1 is able to infer the type and shows no warnings or errors.

IEnumerable<T> ExecuteCommand<T>(
    string commandText,
    string connectionName = null,
    Func<IDataRecord, T> converter = null) { ... }

static SomeClass Create(IDataRecord record) { return new SomeClass(); }

void CannotInferType() {
    var a = ExecuteCommand(
        "SELECT blah",
        "connection",
        converter: Test.Create);
}

void CanInferType() {
    var a = ExecuteCommand(
        "SELECT blah",
        "connection",
        Test.Create);
}

Calling it as described in CannotInferType and when attempting to compile it the compiler emits error CS0411: The type arguments for method 'Test.ExecuteCommand<T>(string, string, System.Func<System.Data.IDataRecord,T>)' cannot be inferred from the usage. Try specifying the type arguments explicitly. Whereas calling it as described in CanInferType works as expected.

As stated above, Visual Studio itself reports no problems, and intellisense for the variable a shows IEnumerable<SomeClass> as expected but for some reason it doesn't compile.

like image 607
Joshua Avatar asked Mar 27 '12 22:03

Joshua


1 Answers

It was a bug in the C# 4 compiler. It's been fixed in the C# 5 compiler.

I suspect it's not the optional parameter which is causing the problem here - it's the named argument. Try removing the default values for your parameters and I suspect you'll still have the same problem. (It's worth differentiating between optional parameters and named arguments - they're two separate features. They're often used together, but certainly don't have to be.)

That's the conclusion I came to when I sent this bug report to Eric and Mads:

using System;

class Test
{
    static void Foo<T>(Func<T> func) {}

    static void Main()
    {
        // Works fine
        Foo(() => "hello");

        // Type inference fails
        Foo(func: () => "hello");
    }
}

Happily this now works in the C# 5 beta compiler.

like image 51
Jon Skeet Avatar answered Sep 22 '22 11:09

Jon Skeet