Q: Populating a structure with RTTI

Chris Nicholson-Sauls ibisbasenji at gmail.com
Sun Jun 10 20:48:41 PDT 2007


Myron Alexander wrote:
> Hello.
> 
> Is it possible to populate a struct using RTTI?
> 
> Example:
> 
> Say I have a struct as such:
> 
> struct Example {
>    int    x;
>    int    y;
>    char[] z;
> }
> 
> and I want to create and populate the structure from a function:
> 
> T populate(T) () {
>   T t;
>   t.field[0] = 1;
>   t.field[1] = 2;
>   t.field[2] = "testing";
>   return t;
> }
> 
> void main() {
>   auto x = populate!(Example)();
> }
> 
> Is this possible? If so, what is the syntax?
> 
> Thanks ahead,
> 
> Myron.

T populate (T) () {
   T t;
   t.tupleof[0] = 1;
   t.tupleof[1] = 2;
   t.tupleof[2] = "testing";
   return t;
}

Although its dangerous, but I'm sure you knew that already.

-- Chris Nicholson-Sauls


More information about the Digitalmars-d-learn mailing list