In C #, you can create higher-order functions, i.e. functions gtake functions as arguments. Let's say I want to create a function that defines a function fand returns another function that extends its functionality. How to determine argument names for returned extended method? The motivation is that I work with higher-order methods in general, some of which create new methods .. and this can be difficult to use because parameter names are not attached to them, etc.
An example illustrating how gand faccordingly can be defined in C #:
I define an Extend method that can extend methods that take an argument Tas an argument and return S.
static class M
{
public static Func< T, S> Extend(Func< T, S> functionToWrap)
{
return (someT) =>
{
...
var result = functionToWrap(someT);
...
return result;
};
}
}
Then we can extend the 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 is positiveNumberno longer available, as available information is available only Func<int, int> Calc. That is, when I use the advanced method by typing new Calc2().Calc(-1), I get no help from the IDE, which is actually my argument is incorrect.
It would be nice if we could define delegateand apply it to this, however this is not possible.
Any suggestions?
source
share