Q: Populating a structure with RTTI

Lutger lutger.blijdestijn at gmail.com
Mon Jun 11 08:30:43 PDT 2007


Lutger wrote:
> Myron Alexander wrote:
>>>       /+ Won't compile:
>>>       for (int i = 0; i < T.tupleof.length; i++) {
>>>          t.tupleof[i] = r.t[i];
>>>       }
>>>       +/
> 
> You can use foreach on tuples:
> 
> foreach(index, value; r.t)
>     t.tupleof[index] = value;


I missed the other one, this could be done in a similar fashion, for 
example:

/+ Won't compile:
    int i = 0;
    r.t[i++] = "Myron";
    r.t[i++] = 10;
    r.t[i++] = "addr1";
    r.t[i++] = "addr2";
    r.t[i++] = "addr3";
    r.t[i++] = 100001.10;
+/


with a helper function:

void populateRow(R, T...)(inout R r, T t)
{
     static assert(is(typeof(r.t) == T));
     foreach(index, value; t)
         r.t[index] = value;
}


inside fetchOne:

populateRow(r, "Myron"[], 10, "addr1"[], "addr2"[], "addr3"[], 100001.10);


More information about the Digitalmars-d-learn mailing list