Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
January 19, 2018 Local static variables must have unique names within a function's scope. | ||||
---|---|---|---|---|
| ||||
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 |
January 19, 2018 Re: Local static variables must have unique names within a function's scope. | ||||
---|---|---|---|---|
| ||||
Posted in reply to tipdbmp | 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
|
January 19, 2018 Re: Local static variables must have unique names within a function's scope. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjærås | > 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.
|
January 19, 2018 Re: Local static variables must have unique names within a function's scope. | ||||
---|---|---|---|---|
| ||||
Posted in reply to tipdbmp | 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 |
January 19, 2018 Re: Local static variables must have unique names within a function's scope. | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | 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 |
January 19, 2018 Re: Local static variables must have unique names within a function's scope. | ||||
---|---|---|---|---|
| ||||
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
|
Copyright © 1999-2021 by the D Language Foundation