Iterate over symbols in tupleof without instance

Dicebot public at dicebot.lv
Tue Apr 15 05:22:15 PDT 2014


On Tuesday, 15 April 2014 at 12:11:52 UTC, Artur Skawina wrote:
> On 04/15/14 13:33, Dicebot wrote:
>> On Tuesday, 15 April 2014 at 11:25:14 UTC, Artur Skawina wrote:
>>>    void iterate(T)()
>>>    {
>>>        foreach (index, member; typeof(T.tupleof))
>>>        {
>>>            pragma(msg, __traits(identifier, 
>>> T.tupleof[index]));
>>>        }
>>>    }
>> 
>> So ashamed :D
>
> I would have not thought of that either, if Jacob didn't 
> mention it... :)
>
> http://forum.dlang.org/post/jl9gil$i9l$1@digitalmars.com
>
> artur

What I mean is that my initial statement is blatantly wrong - of 
course D does support iteration over arbitrary entities, because 
it does support iteration over  TypeTuple!(...). Must have been 
some sort of brainfart.

This trick does make some sense if you think about it. T.tupleof 
is a list of aggregate field symbols which are normally resolved 
as values. But you need a context pointer to work with aggregate 
fields so naive value iteration is not possible. This behavior is 
necessary to support idioms like this:

int a, b;
TypeTuple!(a, b)[] = TypeTuple!(42, 42)[];

Error message could have been better though.


More information about the Digitalmars-d-learn mailing list