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?
It's called scope resolution operator. More information in Scope Resolution Operator (::) (PHP manual).
In C++, scope resolution operator is ::. It is used for following purposes. 2) To define a function outside a class. 3) To access a class's static variables.
An operator, in Java, is a special symbols performing specific operations on one, two or three operands and then returning a result. The operators are classified and listed according to precedence order. Java operators are generally used to manipulate primitive data types.
The ::() operator in Scala is utilized to add an element to the beginning of the List. Method Definition: def ::(x: A): List[A] Return Type: It returns the stated list after adding an element to the beginning of it.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With