In my application, I want a function that after doing some work, return a function with the same signature as itself, or null: Notice that there's nothing generic here, as all type are "static" (as in, the opposite of generic)
//type is not a C# keyword, but bear with me
type RecursiveType = Func<int, RecursiveType>;
RecursiveType currentStep; //something not null
var i = 0;
while (currentStep != null) {
currentStep = currentStep(i);
i += 1;
}
"currentStep" would be something like (this is an example. In the real case Foo::A executes some logic to decide which function it will return, and may or may not be itself)
class Foo {
public static RecursiveType fun(int x) {
if (x < 3) {
return Foo.A
}
else {
return null;
}
}
}
Is this possible in C#?
You can declare a delegate type like this:
public delegate RecursiveType RecursiveType(int x);
Then this will compile:
RecursiveType currentStep = Foo.fun(1);
var i = 0;
while (currentStep != null)
{
currentStep = currentStep(i);
i += 1;
}
The delegate represents a function that accepts an int
and returns a function of the same signature.