Hopefully someone can explain this to me. Sorry if it's a repeat, the keywords to explain what I'm seeing are beyond me for now..
here is some code that compiles
class Program
{
static void Main(string[] args)
{
new Transformer<double, double>(Math.Sqrt);
}
}
class Transformer<Tin, Tout>
{
Func<Tin, Task<Tout>> actor;
public Transformer(Func<Tin, Tout> actor)
{
this.actor = input => Task.Run<Tout>(() => actor(input));
}
}
and here is some code that does not
class Program
{
static void Main(string[] args)
{
new Transformer<double, double>(Math.Sqrt);
}
}
public class Transformer<Tin, Tout>
{
Func<Tin, Task<Tout>> actor;
public Transformer(Func<Tin, Tout> actor)
{
this.actor = input => Task.Run<Tout>(() => actor(input));
}
public Transformer(Func<Tin, Task<Tout>> actor)
{
this.actor = actor;
}
}
By adding the constructor overload, this apparently creates ambiguity but I'm not sure why. Math.Sqrt is not overloaded and clearly has a return type of double, not Task<double>.
Here is the error:
The call is ambiguous between the following methods or properties: 'ConsoleApplication1.Transformer<double,double>.Transformer(System.Func<double,double>)' and 'ConsoleApplication1.Transformer<double,double>.Transformer(System.Func<double,System.Threading.Tasks.Task<double>>)'
Can someone explain why the choice is not obvious to the compiler?
Easy workaround for those who care:
class Program
{
static void Main(string[] args)
{
new Transformer<double, double>(d => Math.Sqrt(d));
}
}