www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - static foreach declaration already defined

reply Ssenni <ssenni gmail.com> writes:
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 {".
Aug 27
next sibling parent "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
On 27/08/2026 9:40 PM, Ssenni wrote:
 If I recall, there used to be a trick for static foreach using double 
 braces, but the compiler complains "declaration expected not {".
Double braces isn't a thing. What you've seen is a scope statement. ```d void func() { { } } ``` Since you're working as a declaration of course it won't work.
Aug 27
prev sibling next sibling parent reply drug007 <drug2004 bk.ru> writes:
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 } ```
Aug 27
parent drug007 <drug2004 bk.ru> writes:
Ah, sorry, I rushed
Aug 27
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 27 August 2026 at 09:40:15 UTC, Ssenni wrote:
 How can I get around this?
Use OpenD instead. We fix obvious bugs like this to make the language work the way it should.
Aug 27
parent reply Ssenni <ssenni gmail.com> writes:
On Thursday, 27 August 2026 at 11:15:49 UTC, Adam D. Ruppe wrote:
 Use OpenD instead. We fix obvious bugs like this to make the 
 language work the way it should.
I do use OpenD! I wanted to make it work with both compilers. I'll just use a template instead.
Aug 27
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 27 August 2026 at 11:29:10 UTC, Ssenni wrote:
 I'll just use a template instead.
yeah, wrapper template of the is() thing is really the only way to do it in upstream.
Aug 27