I'm going to strike a guess at my own question... does the line commented out below fail to compile because methods and funcs are two different things?
Is there any way to avoid typing <string>
? If not, is there a reason why? And if I'm right that it is because methods don't cast to funcs or vice-versa, how could the solution to inferring the type of T
from Method Test.A
be anything other than string
in this example?
Forgive me, but it seems strange that I can clearly pass the method Test.A
as an argument satisfying another method's parameter signature Func<T, T>
(implying in my mind they are interchangeable)... but I don't get any inference for T
at the call site.
public static class Test
{
public static Func<T, T> TestFunc<T>(Func<T, T> input)
{
return input;
}
public static string A(string val)
{
return val;
}
}
public class TestStuff
{
[Fact]
public static void TestMe()
{
var expected = Guid.NewGuid().ToString();
// this fails to compile with
// 'type arguments for method ... cannot be inferred from usage'
// var actual = Test.TestFunc(Test.A);
// this succeeds
var actual = Test.TestFunc<string>(Test.A);
// test passes proving a method satisfies a Func<T, T> parameter
Assert.Equal(expected, actual(expected));
}
}