www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Dot syntax to access static variables of functions

reply bearophile <bearophileHUGS lycos.com> writes:
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
Dec 15 2011
next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
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.
Dec 16 2011
prev sibling parent reply Sean Kelly <sean invisibleduck.org> writes:
On Dec 15, 2011, at 3:48 PM, bearophile wrote:

 In some cases I use a global variable only from a small small number =

=20
=20
 import std.stdio;
 __gshared static int x =3D 10;
 void foo() {
    // uses x
    writeln("foo");
 }
 void main() {
    auto fptr =3D &foo;
    fptr();
    auto y =3D x; // uses x
 }
=20
=20
 To write more tidy code in some of those situations I'd like a dot =

=20
 void foo() {
    __gshared static int x =3D 10;
    // uses x
    writeln("foo");
 }
 void main() {
    auto fptr =3D &foo;
    fptr();
    auto y =3D foo.x; // uses foo.x
 }

struct foo { __gshared int x =3D 10; static void opCall() { // uses x writeln("foo"); } } void main() { auto fptr =3D foo; // sad that the syntax has to change here fptr(); auto y =3D foo.x; // uses foo.x }=
Dec 16 2011
parent reply bearophile <bearophileHUGS lycos.com> writes:
Timon Gehr:

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

What I have proposed is just about static variables of functions, not all local variables. ----------------- Sean Kelly:
 struct foo {
     __gshared int x = 10;
     static void opCall() {
         // uses x
         writeln("foo");
     }
 }
 
 void main() {
     auto fptr = foo; // sad that the syntax has to change here
     fptr();
     auto y = foo.x; // uses foo.x
 }

I don't know what you were trying to express here (and struct names start with an upper case in D). Thank you for your answers, bearophile
Dec 16 2011
parent Walter Bright <newshound2 digitalmars.com> writes:
On 12/16/2011 5:37 PM, bearophile wrote:
 Timon Gehr:

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

What I have proposed is just about static variables of functions, not all local variables.

Then you'd have to add marking them with "public" and "private". It doesn't seem worth the effort.
Dec 16 2011