static foreach declaration already defined

drug007 drug2004 at bk.ru
Thu Aug 27 09:48:26 UTC 2026


On 8/27/26 12:40, Ssenni wrote:
> How can I get around this?
> 
> ```
> struct Foo(T) {
> 
>    static foreach (m; __traits(derivedMembers, T)) {
>      static foreach (i, method; __traits(getOverloads, T, m)) {
>        static if (is(typeof(method) Params == __parameters)) {
>        }
>      }
>    }
> }
> ```
> 
> Error: declaration `Params` is already defined here.
> 
> If I recall, there used to be a trick for static foreach using double 
> braces, but the compiler complains "declaration expected not {".
> 
> 

This is because in D static foreach does not create new scope and all 
iterations are executed within the same scope, which leads to the error. 
You need to explicitly add the scope:
```
static foreach(..){
     static foreach (...) {{  // <-- note double curly braces
       ...
     }}                       // <-- note double curly braces
}
```


More information about the Digitalmars-d-learn mailing list