getSymbolsByUDA in constructor/member functions
Arafel
er.krali at gmail.com
Thu Jun 16 09:29:36 UTC 2022
On 16/6/22 10:55, frame wrote:
> On Thursday, 16 June 2022 at 08:23:20 UTC, Arafel wrote:
>
>
> This is not true. `getMember` can return the symbol to the instance or
> the type/alias, depending if you pass `this` or `Def`. The last is static.
>
> It makes no sense to use the attribute from a class without an instance.
>
>
Classes can have static members just as structs, so I don't think you
always need an instance for a class either.
It seems the issue could be somewhere else:
```
import std.traits: getSymbolsByUDA;
enum E;
class C {
@E int a;
pragma(msg, __traits(getMember,C,"a").stringof); // `a`
void foo() {
pragma(msg, C.stringof); // `C`
pragma(msg, __traits(getMember,C,"a").stringof); // `this.C.a`
// Fails here
//static foreach (sym; getSymbolsByUDA!(C, E)) { }
}
// But works here
static foreach (sym; getSymbolsByUDA!(C, E)) { }
}
```
So if you call `getMember` from a member function, it adds the hidden
`this` reference, and this has subtle consequences later on, even if
`this.C` is practically just an alias for `C`.
I still think this is a bug in `getMember`, although perhaps not as
obvious as I first thought.
More information about the Digitalmars-d-learn
mailing list