Getting only the data members of a type
Ary Manzana
ary at esperanto.org.ar
Sun Apr 1 17:43:18 PDT 2012
On 4/1/12 8:09 PM, Jacob Carlborg wrote:
> On 2012-04-01 08:18, Ali Çehreli wrote:
>> On 03/31/2012 09:09 PM, Artur Skawina wrote:
>
>> >
>> > 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.
>
> Yeah, it's a bit strange. One could think that it would be possible to
> use "m.stringof" but that just returns the type. Instead of using
> "s.tupleof" it's possible to use "typeof(S.tupleof)".
>
> Have a look at:
>
> https://github.com/jacob-carlborg/orange/blob/master/orange/util/Reflection.d#L212
>
>
> It's possible to get the type of a field as well, based on the name:
>
> https://github.com/jacob-carlborg/orange/blob/master/orange/util/Reflection.d#L237
This is what I don't like about D. It gives you a hammer and everyone
tries to solve all problems with that single hammer. Then you get
duplicated code for basic stuff, like getting the type of a field, in
many projects.
It's a waste of time for a developer to have to sit down and think how
we can cheat the compiler or make it talk to give us something it
already knows, but only having a hammer to do so.
Either put that in the language, or in the core library. But don't make
people waste time.
I'd suggest sending pull request with methods that accomplish those
annoyances.
On the other hand, take a look at the implementation of std.traits. Is
it really a win to implement functionLinkage in D? Right here:
https://github.com/D-Programming-Language/phobos/blob/master/std/traits.d#L704
you are repeating the linkages with their names, when that information
is already available to the compiler. What's the point in duplicating
information? The compiler already knows it, and much better than D. It
could be implemented in a much simpler way. Is it just the pride of
saying "Look what I can do with my powerful compile time reflection
capabilities (basically stringof in that module)?"
I'm not angry, but I don't think things are taking the correct direction...
More information about the Digitalmars-d-learn
mailing list