www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Local static variables must have unique names within a function's

reply tipdbmp <email example.com> writes:
The following seems to work in C++, but errors in D, why is that?

int foo(int* num) {
     {
         static int x = 10;
         x += 1;
         *num += x;
     }

     {
         static int x = 20; // error: foo.x is already defined in 
another scope in foo
         x += 2;
         *num += x;
     }

     return 0;
}

https://dlang.org/spec/function.html#local-static-variables
Jan 19 2018
parent reply Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Friday, 19 January 2018 at 11:02:01 UTC, tipdbmp wrote:
 The following seems to work in C++, but errors in D, why is 
 that?

 int foo(int* num) {
     {
         static int x = 10;
         x += 1;
         *num += x;
     }

     {
         static int x = 20; // error: foo.x is already defined 
 in another scope in foo
         x += 2;
         *num += x;
     }

     return 0;
 }

 https://dlang.org/spec/function.html#local-static-variables
19.17.1.3 explains the fact that they need to have unique names, but doesn't provide a reason. Mostly, it's just a bad idea - it's very easy for a person reading the code after you've written it to get the two x's mixed up. -- Simen
Jan 19 2018
parent reply tipdbmp <email example.com> writes:
 Mostly, it's just a bad idea - it's very easy for a person 
 reading the code after you've written it to get the two x's 
 mixed up.
// example from: 19.17.1.3 void main() { { static int x; } { static int x; } // error { int i; } { int i; } // ok } I don't really see how the 'static' storage class would make 2 variables with the same name in different scopes "easier to mix up" compared to 2 variables with the same name in different scopes but declared without the 'static' keyword.
Jan 19 2018
next sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Fri, Jan 19, 2018 at 02:16:24PM +0000, tipdbmp via Digitalmars-d-learn wrote:
 Mostly, it's just a bad idea - it's very easy for a person reading
 the code after you've written it to get the two x's mixed up.
// example from: 19.17.1.3 void main() { { static int x; } { static int x; } // error { int i; } { int i; } // ok } I don't really see how the 'static' storage class would make 2 variables with the same name in different scopes "easier to mix up" compared to 2 variables with the same name in different scopes but declared without the 'static' keyword.
I think this is more an implementational quirk than any deliberate decision on variable names being "easier to mix up". A static variable currently mangles to a combination of the function name and variable name, so if there are multiple static variables with the same name, their mangled names would collide. OTOH, it would really suck if non-static variable names can't be reused in nested scopes, because it would mean you have to invent new names for, say, loop variables for every loop in the same function: void func(int[100] a, int[100] b) { foreach (i; 0 .. 100) { a[i] = i; } foreach (i; 0 .. 100) { b[i] = i; } // this would fail } Fortunately this is not the case. However, the static variable case is annoying, and it's actually one case of a larger problem: void main() { foreach (i; 0 .. 10) { struct S { int x; } auto s = S(i); } foreach (i; 11 .. 20) { struct S { // <---- this is line 9 int y; } auto s = S(i); } } The compiler says: test.d(9): Error: declaration S is already defined in another scope in main even though the respective scopes of the declarations are disjoint. IMO, this is a needless, arbitrary restriction. Worse yet, the compiles ICEs after this, so I filed a bug: https://issues.dlang.org/show_bug.cgi?id=18266 T -- Latin's a dead language, as dead as can be; it killed off all the Romans, and now it's killing me! -- Schoolboy
Jan 19 2018
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 1/19/18 1:11 PM, H. S. Teoh wrote:

 Fortunately this is not the case.  However, the static variable case is
 annoying, and it's actually one case of a larger problem:
 
 	void main() {
 		foreach (i; 0 .. 10) {
 			struct S {
 				int x;
 			}
 			auto s = S(i);
 		}
 		foreach (i; 11 .. 20) {
 			struct S { // <---- this is line 9
 				int y;
 			}
 			auto s = S(i);
 		}
 	}
 
 The compiler says:
 
 	test.d(9): Error: declaration S is already defined in another scope in main
 
 even though the respective scopes of the declarations are disjoint. IMO,
 this is a needless, arbitrary restriction. Worse yet, the compiles ICEs
 after this, so I filed a bug:
 
 	https://issues.dlang.org/show_bug.cgi?id=18266
Related: https://issues.dlang.org/show_bug.cgi?id=17653 -Steve
Jan 19 2018
prev sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Friday, January 19, 2018 10:11:36 H. S. Teoh via Digitalmars-d-learn 
wrote:
 Fortunately this is not the case.  However, the static variable case is
 annoying, and it's actually one case of a larger problem:

   void main() {
       foreach (i; 0 .. 10) {
           struct S {
               int x;
           }
           auto s = S(i);
       }
       foreach (i; 11 .. 20) {
           struct S { // <---- this is line 9
               int y;
           }
           auto s = S(i);
       }
   }

 The compiler says:

   test.d(9): Error: declaration S is already defined in another scope in
 main

 even though the respective scopes of the declarations are disjoint. IMO,
 this is a needless, arbitrary restriction. Worse yet, the compiles ICEs
 after this, so I filed a bug:

   https://issues.dlang.org/show_bug.cgi?id=18266
This limitation gets especially annoying when you start doing stuff like generating tests using static foreach. In those cases, any helper types or functions that you have can't be inside the static foreach. To an extent, you can get around it using a templates outside of the static foreach, but it's still annoying. - Jonathan M Davis
Jan 19 2018