In C# it is possible to create higher order functions, ie. functions g
taking functions as arguments. Say I want to create such a function which given a function f
and returns another function extending its functionality. How do I define argument names for the returned enhanced method? The motivation being, that I'm working with higher order methods in general, some of which produce new methods.. and these can be difficult to use as there is no parameter names etc. attached to them.
An example illustrating how g
and f
respectively could be defined in C#:
I define a method Extend that can extens methods taking a T
as argument and returning an S
.
static class M
{
public static Func< T, S> Extend(Func< T, S> functionToWrap)
{
return (someT) =>
{
...
var result = functionToWrap(someT);
...
return result;
};
}
}
We can then extend a method on our class without changing the method.
class Calc2
{
public Func< int, int> Calc;
public Calc2()
{
Calc = M.Extend< int, int>(CalcPriv);
}
private int CalcPriv(int positiveNumber)
{
if(positiveNumber < 0) throw new Exception(...);
Console.WriteLine("calc " + i);
return i++;
}
}
Alas, the argument name positiveNumber
is no longer available, since the only available information is Func<int, int> Calc
. That is when I use the extended method by typing new Calc2().Calc(-1)
I get no help from the IDE that in fact my argument is wrong.
It would be nice if we could define a delegate
and cast it to this, however, this is not possible.
Any suggestions?