Variable notation
Bradley Chatha
sealabjaster at gmail.com
Sun Dec 1 13:48:17 UTC 2024
On Sunday, 1 December 2024 at 12:54:27 UTC, Joan wrote:
> Hello, how do i get all the members that are not marked by
> DO_NOT_CHECK on Check() ?
The following should work:
```d
void Check(T)(T obj) {
import std.stdio : writeln;
import std.traits : hasUDA;
static if (is(T : Object)) {
auto members = obj.tupleof;
foreach (i, member; members) {
static if(hasUDA!(obj.tupleof[i], DO_NOT_CHECK))
writeln("DO NOT CHECK: ", i);
else
writeln("CHECK: ", i);
}
}
}
```
It may be worth giving `std.traits` a check over, since it has
other goodies like
https://dlang.org/phobos/std_traits.html#getSymbolsByUDA
One oddity about D is that UDA information is _very_ easy to
lose. You'd naturally think that you could do `hasUDA!(member,
DO_NOT_CHECK)`, but unfortunately `member` no longer contains any
information about its UDAs - it just becomes a normal `int`, so
you have to directly index `obj.tupleof` in order to reference
the original symbol.
You can see this more clearly with the following function:
```d
void Check(T)(T obj) {
import std.stdio : writeln;
import std.traits : hasUDA;
static if (is(T : Object)) {
auto members = obj.tupleof;
foreach (i, member; members) {
pragma(msg, __traits(getAttributes, member));
pragma(msg, __traits(getAttributes, obj.tupleof[i]));
}
}
}
```
Which will print (I added the comments):
```
AliasSeq!() // Member n - using member
AliasSeq!() // Member n - using obj.tupleof
AliasSeq!() // Member x - using member
AliasSeq!() // Member x - using obj.tupleof
AliasSeq!() // Member t - using member
AliasSeq!((DO_NOT_CHECK)) // Member t - using obj.tupleof
```
Hope that helps.
More information about the Digitalmars-d
mailing list