Search code examples
global-variablesdname-lookup

what is the equivalent of :: operator in D?


I've just started learning D. In C++ there is :: (Scope resolution operator) to access global variable from the function if both global & local varible have same name. But how to do this in D language? Consider this program.

import std.stdio;
int a;
int main(string[] args)
{
    int a=3;
    writeln("D is nice");
    static int i;
    writeln("value of i is: ",i);
    writeln("value of a is: ",a);
   // writeln("value of ::a is: ",::a); compiler error here
    return 0;
}

How can I print the value of global variable a from within main() function? Does D provide such kind of operator?


Solution

  • D uses a leading dot for that:

    writeln("value of .a is: ",.a);
    

    In the spec: http://dlang.org/module.html - section "Module Scope Operator"