views:

51

answers:

1

I want to write code like this:

/*something*/ Fn() { ... }

int main()
{
  /*something*/ fn = Fn;
  while(fn) fn = fn();
  return 0;
}

Is it possible to do this is a fully type safe way? Assume C, C++, D, C#, Java, or any other statically typed language.

+1  A: 

Here is a C# example

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;
    }
}
matt-dot-net
C# I presume???
BCS
Yes, sorry I mentioned that in my orig. post and had to edit for formatting. In my opinion it's a little more straight forward in C/C++ but C# gives you strong typing. With C/C++ a pointer can point to anything and anywhere.
matt-dot-net