Skip to content

TReturn of Func<T, TResult> cannot be inferred in if T is value tuple. #24781

@kalaninja

Description

@kalaninja

I'am not sure if it is actually a bug or not, but this behaviour seems strange to me. Please, consider the following code:

public interface IDo<T>
{
    IResult<TReturn> Do<TReturn>(Func<T, TReturn> fn);
}

public interface IResult<TReturn>
{
}

public class Class1
{
    public void Test1(IDo<(int, int)> impl)
    {
        var case1 = impl.Do<int>(((int x, int y) a) => a.x * a.y); // compiles
        var case2 = impl.Do(a =>
        {
            (var x, var y) = a;
            return x * y;
        }); // compiles

        var case3 = impl.Do(((int x, int y) a) => a.x * a.y); // <-- does not compile
    }

    public void Test2(IDo<(int x, int y)> impl)
    {
        var case4 = impl.Do(a => a.x * a.y); // compiles
    }
}

Can anyone explain why case3 fails with error

Class1.cs(25, 27): [CS0411] The type arguments for method 'IDo<(int, int)>.Do(Func<(int, int), TReturn>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

I don't understand why the compiler cannot infer the return type automatically.

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions