Thread overview
Variable notation
Dec 01
Joan
December 01

enum DO_NOT_CHECK;

class Test {
    int n;
    int x;
    @DO_NOT_CHECK int t;

    this() {
        n = 1;
        x = 2;
        t = 3;
    }
}


void Check(T)(T obj) {
    static if (is(T : Object)) {
        auto members = obj.tupleof;
        foreach (i, member; members) {
            how do i get all the members that are not marked by DO_NOT_CHECK ?
        }
    }
}

void main() {
    auto test = new Test();
    Check(test);
}

Hello, how do i get all the members that are not marked by DO_NOT_CHECK on Check() ?

December 01

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:

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:

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.

December 01

On Sunday, 1 December 2024 at 12:54:27 UTC, Joan wrote:

>

...

Just realised as well that this is in the General group.

It'd be best to ask questions like this in the Learn group in the future :)