Getting only the data members of a type

Artur Skawina art.08.09 at gmail.com
Sun Apr 1 02:40:24 PDT 2012


On 04/01/12 11:27, Artur Skawina wrote:
> On 04/01/12 08:18, Ali Çehreli wrote:
>> On 03/31/2012 09:09 PM, Artur Skawina wrote:
>>> This will print all fields of struct/class S:
>>>
>>>     enum s = cast(S*)null;
>>>     foreach (i, m; s.tupleof) {
>>>        enum name = S.tupleof[i].stringof[4..$];
>>>        alias typeof(m) type;
>>>        writef("(%s) %s\n", type.stringof, name);
>>>     }
>>>
>>> Real Programmers don't use std.traits. ;)
>>>
>>> artur
>>
>> Your method works but needing to iterate on a struct variable by s.tupleof and having to use the struct type as S.tupleof in the loop body is strange.
> 
> That's because the compiler won't accept "foreach (i, t; S.tupleof)" and 
> "*.tupleof[i].stringof" is necessary to get the original name. This would
> have worked too:
> 
>       enum name = *s.tupleof[i].stringof[4..$];
> 
> but obfuscates the code more and looks like dereferencing a null pointer.

That should have been:

      enum name = typeof(*s).tupleof[i].stringof[4..$];

artur


More information about the Digitalmars-d-learn mailing list