我想写这样的代码:
/*something*/ Fn() { ... }
int main()
{
/*something*/ fn = Fn;
while(fn) fn = fn();
return 0;
}
是否有可能这样做是一种完全类型安全的方式?假设 C、C++、D、C#、Java 或任何其他静态类型语言。
我想写这样的代码:
/*something*/ Fn() { ... }
int main()
{
/*something*/ fn = Fn;
while(fn) fn = fn();
return 0;
}
是否有可能这样做是一种完全类型安全的方式?假设 C、C++、D、C#、Java 或任何其他静态类型语言。
这是一个 C# 示例
delegate MyFunctionDelegate MyFunctionDelegate();
class Program
{
static void Main(string[] args)
{
MyFunctionDelegate fn = FN1;
while (fn!=null)
fn = fn();
}
static MyFunctionDelegate FN1()
{
Console.WriteLine("FN1 called");
MyFunctionDelegate returnDelegate = FN2;
return returnDelegate;
}
static MyFunctionDelegate FN2()
{
Console.WriteLine("FN2 called");
return null;
}
}