Local static variables must have unique names within a function's scope.

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sat Jan 20 00:31:32 UTC 2018


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



More information about the Digitalmars-d-learn mailing list