Should getSymbolsByUDA work with member variables?

Steven Schveighoffer schveiguy at gmail.com
Sat Feb 29 20:04:12 UTC 2020


On 2/28/20 1:34 PM, cc wrote:
> This compiles:
> 
> class Foo {
>      int x;
>      @(1) void y() {}
>      this() {
>          static foreach (idx, field; getSymbolsByUDA!(Foo, 1)) {
>          }
>      }
> }
> 
> This does not:
> 
> class Foo {
>      @(1) int x;
>      void y() {}
>      this() {
>          static foreach (idx, field; getSymbolsByUDA!(Foo, 1)) {
>          }
>      }
> }
> 
> Error: value of `this` is not known at compile time
> 
> Is there an equivalent for getSymbolsByUDA for member variables, or is 
> this a bug?

It does work. What doesn't work is using the symbol or aliasing it 
inside a member function.

class Foo {
     @(1) int x;
     void y() {}
     this() {
     }
}

static foreach(sym; getSymbolsByUDA!(Foo, 1))
     pragma(msg, sym.stringof); // prints x


But using foreach  (not static) works inside the function. I'm not sure 
of the difference. I think it's confused because the symbols link to a 
real item inside a member, whereas they are just symbols outside. So it 
might think you want the instance member, which can't be accessed at 
compile time? But I don't know why.

You should file a bug and see what is said about it. The fact that 
foreach on such a tuple works but static foreach doesn't is telling.

-Steve


More information about the Digitalmars-d-learn mailing list