digitalmars.D - Dot syntax to access static variables of functions
- bearophile (40/40) Dec 15 2011 In some cases I use a global variable only from a small small number of ...
- Timon Gehr (3/43) Dec 16 2011 Local variables are not part of the function interface. They are
- Sean Kelly (15/43) Dec 16 2011 syntax to access static variables of a function:
- bearophile (7/22) Dec 16 2011 What I have proposed is just about static variables of functions, not al...
- Walter Bright (3/7) Dec 16 2011 Then you'd have to add marking them with "public" and "private".
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
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, bearophileLocal variables are not part of the function interface. They are implementation details.
Dec 16 2011
On Dec 15, 2011, at 3:48 PM, bearophile wrote:In some cases I use a global variable only from a small small number =of functions, like foo() and main() here:=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 =syntax to access static variables of a function:=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
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
On 12/16/2011 5:37 PM, bearophile wrote:Timon Gehr:Then you'd have to add marking them with "public" and "private". It doesn't seem worth the effort.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.
Dec 16 2011