I'm working with a function that takes two functions as parameters, and returns a new composed one:
public static Action<T> Compose<T>(Action<T> first, Action second)
{
return new Action<T>(arg =>
{
first(arg);
second();
});
}
I've noticed that the compiler complains if I don't specify T
, when sending it a static or member function (as opposed to an actual Action<T>
object):
static void Main(string[] args)
{
// compiler error here
var composed = Compose(Test, () => Console.WriteLine(" world"));
composed("hello");
Console.ReadLine();
}
public static void Test(string arg)
{
Console.Write(arg);
}
The error message:
The arguments for method 'ConsoleTest.Program.Compose(System.Action, System.Action)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
My question: Why can't the type argument be inferred here? The signature of Test
is known at compile time, is it not? Is there really some function you could put in place of Test
, that would cause its signature to be ambiguous?
Footnote: I know I can simply send new Action<string>(Test)
instead of Test
to Compose
(as noted in this question) -- my question is "why", not "how can I do this".