Struct Initialization syntax

Neia Neutuladh neia at ikeran.org
Tue Jul 24 17:55:16 UTC 2018


On Monday, 23 July 2018 at 16:26:42 UTC, Seb wrote:
> I personally prefer option 2, but this might be in conflict to 
> named arguments which we hopefully see in the near future too. 
> Hence, I'm leaning forward to proposing Option 1 as the 
> recommended Option for the DIP (that's also what the PoC DMD PR 
> implements). What's your take on this?

If we have named arguments that can be reordered and, when they 
have default values, omitted, we don't really need a special 
struct initialization syntax. We just need the compiler to 
generate the implicit struct constructor in the obvious way, like:

struct S
{
   string a = "field a!";
   int b = 10;
   // compiler-generated
   this(<string a = "field a!", int b = 10>) {...}
}
writeln(S(a: "hello", b: 15));

Similarly, struct initializer syntax everywhere slightly reduces 
the need for named arguments, albeit with some inconvenience:

// named args style
void drawRect(<int x, int y, int width, int height, string 
color>) {}
// struct style
struct DrawRect
{
   int x, y, width, height;
   string color;
}
void drawRect(DrawRect rect) {}



More information about the Digitalmars-d mailing list