Structure initialization
Sheff
sheffmail at mail.ru
Mon May 7 08:59:08 PDT 2007
Olli Aalto Wrote:
> 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.
Hm, that's not exactly what I meant, you see, there may be hundreds of fields in a structure, and I want to initialize only some of them, for example:
struct A
{
int f1 = default_1;
int f2 = default_2;
...
int f100 = default_100;
}
and what I want is to write
f(A(f3:some_other_value,f50:some_another_value));
instead of
f(A(default_1, default_2, some_other_value, default_4, ..., default_49, some_another_value, ..., default_100));
I mean, there may be different combinations, I want to be able to write like this:
f(A(f3:some_other_value,f50:some_another_value));
or like this:
f(A(f12:some_other_value,f7:some_another_value,f13:some_another_value_2));
or etc...
More information about the Digitalmars-d
mailing list