allMembers and getMember traits behavior

Artur Skawina art.08.09 at gmail.com
Fri Jun 21 06:42:29 PDT 2013


On 06/21/13 15:27, Martin wrote:
> On Friday, 21 June 2013 at 13:15:43 UTC, Jacob Carlborg wrote:
>> On 2013-06-21 15:04, Martin wrote:
>>> module test;
>>>
>>> void test()
>>> {
>>>
>>>
>>>     foreach (member; __traits(allMembers, Client))
>>>     {
>>>         pragma(msg, member); // Prints all members fine, including "value"
>>>     }
>>>
>>>     // This line fails
>>>     static if(__traits(getProtection, __traits(getMember, TestClass,
>>> "value")) == "public")
>>>     {
>>>         pragma(msg, "value is public");
>>>     }
>>>     else
>>>     {
>>>         pragma(msg, "value is not public");
>>>     }
>>>
>>> }
>>>
>>> module testclass;
>>>
>>> class TestClass
>>> {
>>> private:
>>>     int value;
>>> }
>>>
>>> When using the allMembers trait, the private "value" member of TestClass
>>> is included, but when trying to retrieve that member using the getMember
>>> trait, it fails to compile with test.d(13): Error: class main.TestClass
>>> member value is not accessible
>>>
>>> Is that intended behavior or a bug? I can't see why the getMember trait
>>> should not be able to retrieve private members when allMembers trait is
>>
>> Can you use a string instead of getMember?
> 
> You mean something like this:
> 
> foreach (member; __traits(allMembers, TestClass))
> {
>     static if (member == "value")
>     {
>         pragma(msg, __traits(getProtection, member));
>     }
> }
> 
> That works, but it prints "public", even though the "value" member is private. Surprisingly strange behavior

   pragma(msg, __traits(getProtection, mixin("TestClass." ~ member)));

artur



More information about the Digitalmars-d mailing list