On 12/16/2011 12:48 AM, bearophile wrote:
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

Local variables are not part of the function interface. They are implementation details.


Reply via email to