Dot syntax to access static variables of functions
    bearophile 
    bearophileHUGS at lycos.com
       
    Thu Dec 15 15:48:50 PST 2011
    
    
  
In some cases I use a global variable only from a small small number of functions, like foo() and main() here:
import std.stdio;
__gshared static int x = 10;
void foo() {
    // uses x
    writeln("foo");
}
void main() {
    auto fptr = &foo;
    fptr();
    auto y = x; // uses x
}
To write more tidy code in some of those situations I'd like a dot syntax to access static variables of a function:
void foo() {
    __gshared static int x = 10;
    // uses x
    writeln("foo");
}
void main() {
    auto fptr = &foo;
    fptr();
    auto y = foo.x; // uses foo.x
}
Its semantics is similar to just a struct with a static field and a static opCall:
import std.stdio;
struct Foo {
    __gshared static int x = 10;
    static void opCall() {
        // uses x
        writeln("foo");
    }
}
void main() {
    auto y = Foo.x;
    auto fptr = &Foo.opCall;
    fptr();
}
The advantage of using the dot syntax is that I don't need to modify the function code and turn it into a struct, with uppercase name, etc.
Bye,
bearophile
    
    
More information about the Digitalmars-d
mailing list