CT filtering of class members

Sjoerd Nijboer dlang at sjoerdnijboer.com
Sun Aug 11 15:27:54 UTC 2019


The following snippet doesn't compile

I am trying to reflect on a class and only do an operation with 
all member functions of a class.
But I can't seem to use a filter to only get the member functions 
out of a type T.

I understand that there are two errors in my snippet.
1) It cannot mixin a `name` because it is a variable from the 
lambda that `filter()` is using.
2) members.filter!(name => !ctorAndDtor.canFind(name)) does not 
filter on symbols defined in ctorAndDtor

How can I fix these problems and return all member functions 
whitout ctor and dtor of a type T?

Code snippet:

void main()
{
     GetFunctionMembers!Foo();
}

void GetFunctionMembers(T)()
{
     enum members = [__traits(derivedMembers, T)];
     pragma(msg, "Functions: " ~ members.stringof);

     enum ctorAndDtor = ["this", "__ctor", "__dtor"];
     enum memberFunctions = members.filter!(name => 
!ctorAndDtor.canFind(name)
                 && mixin("isFunction!(T." ~ name ~ ")"))();

     pragma(msg, memberFunctions);
}

class Foo
{
     bool myBool;
     int k;

     this()
     {
     }

     ~this()
     {
     }

     void bar(int k)
     {
         this.k = k;
     }

     void qux()
     {
     }
}



More information about the Digitalmars-d-learn mailing list