Structure initialization

Olli Aalto oaalto at gmail.com
Mon May 7 03:59:16 PDT 2007


Sheff wrote:
> Hi, everyone!
> Can anybody tell me how to partially initialize a structure in place, i.e:
> I have a structure:
> 
> struct Props
> {
> int flag = 1;
> int state = 0;
> }
> and a function:
> void f(Props p);
> 
> And I want to pass an instance of Props to f with only some fields initialized, right now I have to write like this:
> f(Props(1,1000));
> but I want to be able to write something like this:
> f(Props(state:1000));
> But that doesn't work, is there a way I can make it work ?

You might want to try something like this:

module props;

import tango.io.Stdout;

struct Props
{
     int flag = 1;
     int state = 0;

     public static Props opCall(int state)
     {
         Props p;
         p.state = state;
         return p;
     }
}

int f(Props props)
{
     return props.state;
}

void main()
{
      int state = f(Props(3));
      Stdout(state).newline;
}

O.



More information about the Digitalmars-d mailing list