Traits: getting all *public* members of a class?

Q q at example.com
Sat Jun 8 07:10:54 PDT 2013


On Saturday, 8 June 2013 at 14:06:32 UTC, Andrej Mitrovic wrote:
> On Saturday, 8 June 2013 at 13:57:13 UTC, Q wrote:
>> Hi there, I want to iterate over all the public members of a 
>> class.
>
> It is a known problem. The workaround is to use is(typeof()):
>
> void main() {
>     foreach (mem; __traits(allMembers, Foo))
>     {
>         static if (is(typeof(
>             __traits(getMember, Foo.init, mem)
>         )))
>         {
>             enum prot = __traits(getProtection,
>                             __traits(getMember, Foo.init, mem));
>             static if (prot == "public") {
>                 pragma(msg, mem);
>             }
>         }
>     }
> }

This worked perfectly. Thank you!


More information about the Digitalmars-d-learn mailing list