Thread overview
[Issue 17974] getSymbolsByUDA is returns unusable symbols when used in foreach
Nov 08, 2017
Artem Borisovskiy
Nov 09, 2017
Basile B.
Nov 09, 2017
Artem Borisovskiy
Mar 21, 2020
Basile-z
Dec 17, 2022
Iain Buclaw
November 08, 2017
https://issues.dlang.org/show_bug.cgi?id=17974

--- Comment #1 from Artem Borisovskiy <kolos80@bk.ru> ---
What I meant to say is that it can't even access the symbol to test if it has the attribute. Anyway, here's some more funny (not so at work) stuff:

// main.d
import std.traits;
enum Attr;

static struct FUBAR
{
    @Attr int a;
    void dummy()
    {
        static foreach (symbol; getSymbolsByUDA!(FUBAR, Attr))
            pragma(msg, getUDAs!(symbol, Attr).stringof);
    }
}

void main()
{
    static foreach (symbol; getSymbolsByUDA!(FUBAR, Attr))
        pragma(msg, getUDAs!(symbol, Attr).stringof);
}

Now I'm lost:

src/main.d(10): Error: value of 'this' is not known at compile time
()
(Attr)

But if I make `dummy' static, it works:
(Attr)
(Attr)

The loop works even on module level (as it should). So it doesn't work only in non-static methods of FUBAR.

--
November 09, 2017
https://issues.dlang.org/show_bug.cgi?id=17974

Basile B. <b2.temp@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |b2.temp@gmx.com

--- Comment #2 from Basile B. <b2.temp@gmx.com> ---
(In reply to Artem Borisovskiy from comment #1)
> What I meant to say is that it can't even access the symbol to test if it has the attribute. Anyway, here's some more funny (not so at work) stuff:
> 
> // main.d
> import std.traits;
> enum Attr;
> 
> static struct FUBAR
> {
>     @Attr int a;
>     void dummy()
>     {
>         static foreach (symbol; getSymbolsByUDA!(FUBAR, Attr))
>             pragma(msg, getUDAs!(symbol, Attr).stringof);
>     }
> }
> 
> void main()
> {
>     static foreach (symbol; getSymbolsByUDA!(FUBAR, Attr))
>         pragma(msg, getUDAs!(symbol, Attr).stringof);
> }
> 
> Now I'm lost:
> 
> src/main.d(10): Error: value of 'this' is not known at compile time
> ()
> (Attr)
> 
> But if I make `dummy' static, it works:
> (Attr)
> (Attr)
> 
> The loop works even on module level (as it should). So it doesn't work only in non-static methods of FUBAR.

---
import std.traits;
enum Attr;

struct FUBAR
{
    @Attr int a;
    void dummy()
    {
        foreach (i, symbol; getSymbolsByUDA!(FUBAR, Attr))
            pragma(msg, getUDAs!(getSymbolsByUDA!(FUBAR, Attr)[i], Attr));
    }
    void dummy2()
    {
        static foreach (i; 0..getSymbolsByUDA!(FUBAR, Attr).length)
            pragma(msg, getUDAs!(getSymbolsByUDA!(FUBAR, Attr)[i], Attr));
    }
}

void main()
{
    static foreach (symbol; getSymbolsByUDA!(FUBAR, Attr))
        pragma(msg, getUDAs!(symbol, Attr).stringof);
}
---

Good luck.

--
November 09, 2017
https://issues.dlang.org/show_bug.cgi?id=17974

--- Comment #3 from Artem Borisovskiy <kolos80@bk.ru> ---
Thanks, but I already found another way of doing it, by using __traits(getMember...). Anyway, both my and your solutions are ugly hacks. They make code even less readable than it was before. It's possible to get rid of some repetition, though:

alias symbols = getSymbolsByUDA!(FUBAR, Attr);
static foreach (i; symbols.length.iota)
    pragma(msg, getUDAs!(symbols[i], Attr));

Still, `static foreach (symbol; getSymbolsByUDA!(FUBAR, Attr))' would be nicer.

--
March 21, 2020
https://issues.dlang.org/show_bug.cgi?id=17974

Basile-z <b2.temp@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|b2.temp@gmx.com             |

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=17974

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P3

--
December 01
https://issues.dlang.org/show_bug.cgi?id=17974

--- Comment #4 from dlangBugzillaToGithub <robert.schadek@posteo.de> ---
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/phobos/issues/9724

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB

--