KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Why does the C# compiler not infer the fact that FooExt.Multiply() satisfies the signature of Functions.Apply() ? I have to specify a separate delegate variable of type Func<Foo,int,int> for the code to work ... but it seems like type inference should handle this. Am I wrong? And, if so, why? EDIT: The compilation error received is: The type arguments for method FirstClassFunctions.Functions.Apply<T1,T2,TR>(T1, System.Func<T1,T2,TR>, T2)' cannot be inferred from the usage. Try specifying the type arguments explicitly namespace FirstClassFunctions { public class Foo { public int Value { get; set; } public int Multiply(int j) { return Value*j; } } public static class FooExt { public static int Multiply(Foo foo, int j) { return foo.Multiply(j); } } public static class Functions { public static Func<TR> Apply<T1,T2,TR>( this T1 obj, Func<T1,T2,TR> f, T2 val ) { return () => f(obj, val); } } public class Main { public static void Run() { var x = new Foo {Value = 10}; // the line below won't compile ... var result = x.Apply(FooExt.Multiply, 5); // but this will: Func<Foo, int, int> f = FooExt.Multiply; var result = x.Apply(f, 5); } }
Tags (comma-separated)
Save Edits
Cancel