Hi, I'm trying to get a list of only member functions of a struct. I've found that if you do not declare a struct as static inside a scope, then there's a hidden "this" member as part of the struct. Can someone explain the logic there?

Also am I going about this correctly?

template MemberFunctions(T) {
    import std.traits: isFunction;
    auto MemberFunctions() {
        string[] memberFunctions;
        foreach (member; __traits(allMembers, T)) {

// NOTE: This static if is here is because of that hidden "this" member
            //
            // if I do not do this then I get:
            //   Error: identifier expected following ., not this

            static if (is(typeof(mixin("T." ~ member)) F))
                if (isFunction!F) {
                    memberFunctions ~= member;
                }
        }
        return memberFunctions;
    }
}

unittest {
    // works for static and non static.
    /* static */ struct A {
        void opCall() {}
        void g() {}
    }

    /* static */ struct B {
        int m;
        A a;
        alias a this;
        void f() {}
    }

    static assert(MemberFunctions!B == ["f"]);
}

Cheers

Reply via email to