what is the equivalent of :: operator in D?
Asked Answered
L

1

6

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?

Lipophilic answered 4/8, 2015 at 10:40 Comment(0)
E
13

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"

Emelda answered 4/8, 2015 at 11:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.