Getting the parameters of a struct/class constructor

Rob T alanb at ucora.com
Thu Jan 24 09:54:48 PST 2013


There may be more than one "this", so you'll have to specify the 
args for each specific constructor manually.

Disclaimer: Someone else may have a better solution as I'm not 
that much of an expert in this area.

This sample may point you in the right direction ...

import std.typetuple;

struct X
{
     alias TypeTuple!(int, double) CONSTUCT1;
     alias TypeTuple!(int, double, string) CONSTUCT2;

     this( CONSTUCT1 args )
     {
         alias args[0] a_int;
         alias args[0] a_double;

         _a = a_int;
         _b = b_double;

     }

     this( CONSTUCT2[0] a_int, CONSTUCT2[1] b_double, CONSTUCT2[2] 
c_string  )
     {

         _a = a_int;
         _b = b_double;
         writeln(c_string);
     }

     int _a;
     double _b;

}


void foo(X.CONSTUCT1[0] a, X.CONSTUCT1[1] b )
{

   ...

}

void bar(X.CONSTUCT2 args )
{

    alias args[0] a_int;
    alias args[1] a_double;
    alias args[2] a_string;
   ...

}

Hope this helps.

--rt


More information about the Digitalmars-d-learn mailing list