D Language Traits Iterating over members of a struct/class without their inherited members


Example

module main;

auto getMemberNames(T)() @safe pure {
    string[] members;
    
    foreach (derived; __traits(derivedMembers, T)) {
        members ~= derived;
    }
    
    return members;
}

class Foo {
    int a;
    int b;
}

class Bar : Foo {
    int c;
    int d;
    int e;
}

void main() {
    import std.stdio;
    
    foreach (member; getMemberNames!Bar) {
        writeln(member);
    }
}

derivedMembers returns a tuple of string literals, where each string is the member name.

The example outputs:

c
d
e