Quantcast
Channel: The Fugue » cs
Viewing all articles
Browse latest Browse all 10

Mutually-recursive Nested Functions in D

$
0
0

From http://digitalmars.com/d/2.0/function.html:

Unlike module level declarations, declarations within function scope are processed in order. This means that two nested functions cannot mutually call each other:

void test()
{
    void foo() { bar(); }	// error, bar not defined
    void bar() { foo(); }	// ok
}

The solution is to use a delegate:

void test()
{
    void delegate() fp;
    void foo() { fp(); }
    void bar() { foo(); }
    fp = &bar;
}

Future directions: This restriction may be removed.

You can also do away with the useless name, e.g.

void test()
{
    void delegate() bar;
    void foo() { bar(); }
    bar = delegate void() { foo(); };
}

Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles



Latest Images