Struct initializers as expressions

Mike Parker via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Dec 2 22:38:20 PST 2015


On Thursday, 3 December 2015 at 05:26:17 UTC, Chris Wright wrote:
> I can initialize a struct with named values:
>
> ---
> struct Foo {
>   int i, j, k, l, m, n;
> }
> Foo f = {k: 12};  // other fields get default initialization
> ---
>
> I can initialize it with call syntax:
>
> ---
> auto f = Foo(0, 0, 12, 0, 0, 0);  // works
> ---

The bottom syntax is a struct literal, the top one is not.


>
> Ideally, I want something that lets you specify only the fields 
> you care about and won't have to be modified (just recompiled) 
> if I add more fields. I also want something inline.
>
> Is there anything I can use here that's better than what I have?

AFAIK, your only option is to use a struct constructor. This is 
the sort of thing they're  used for.



More information about the Digitalmars-d-learn mailing list