How to use pure in D 2.0 question on Stack Overflow?

bearophile bearophileHUGS at lycos.com
Thu Jun 18 00:57:46 PDT 2009


Denis Koroskin:
>There needs to be a way to tell that addMsg doesn't access global variables.<
>Another solution is... remove global variables entirely! No globals - no  problems :)<

"global variables" are names in the outer scope for nested functions, so that doesn't solve the problem, and generally kills C compatibility of D.

Time ago I have suggested a keyword like uses or outer, to explicitly list what names a function uses from the outer scope:

int z;
int bar(int k) uses out z {
    int y = k * 2;
    int foo(int x) uses in y, out z {
        z = y * 2;
        return x * y;
    }
    return k + foo(k * 3);
}

But this example is a mess already, better to not encourage the usage of such coding style.

Bye,
bearophile



More information about the Digitalmars-d mailing list