Overload resolution type inference issue with sync and async delegates

  • Thread starter Thread starter Theodor Zoulias
  • Start date Start date
T

Theodor Zoulias

Guest
I have two overloads of the method `Foo`, one for synchronous and one for asynchronous delegates:

public static void Foo<T>(Func<T> function, List<T> list = null) { }

public static void Foo<T>(Func<Task<T>> function, List<T> list = null) { }

This method accepts a `function`, and a second optional argument of type `List<T>`.

When I try to call this method without specifying the type `<T>`, the C# 8 compiler is able resolve the ambiguity:

Foo(async () => { await Task.CompletedTask; return 0;}); // OK, T is int

...unless I attempt to pass explicitly `null` as the second argument:

Foo(async () => { await Task.CompletedTask; return 0;}, null); // Error CS0121

In that case I get a compilation error: The call is ambiguous between the following methods or properties: `'Test.Foo<T>(Func<T>, List<T>)'` and `'Test.Foo<T>(Func<Task<T>>, List<T>)'`

The strange thing is that `null` is the default value of this argument anyway!

Is this a limitation of the compiler that could be fixed, or an unsolvable case of ambiguity that can't be handled in any other way than by a compilation error?

Continue reading...
 
Back
Top