Add initialization lists to D.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/object-and-collection-initializers
it can be useful in many case: JSON, GUI..
auto lbl = new Label { Text: "hello", Color: Colors.red };
and even more
I think that it should be extended like WITH-keyword in some languages
(lbl is variable from code above, it's created already, we just want change some props):
lbl = { Text: "hello again", Color: Colors.green }; // no temporary vars here.
// Text & Color belong to "lbl" variable. same as
lbl.Text = "hello again";
lbl.Color = Colors.green;
// colon":" that means assign"=" looks a little bit weird imo, but like in JSON/JS
or same as "with" keyword in some unknown language
with lbl {
Text = "hello again",
Color = Colors.green,
}
Comment #1 by alphaglosined — 2018-07-24T09:25:46Z
struct p2 { int x, y; }
struct p3 { p2 p; alias p this; int z; }
p3 p = { x:1, y:2, z:3 }; // case (1)
// we've got error: `x` is not a member of `p3`. works only for "z" alone
// try again
p3 p = { p = { x:1, y:2 }, z:3 }; // many another errors
// try again
p3 p = { p.x:1, z:3 }; // many another errors
// try again
p3 p = { z:3 };
p.p.x = 1; // internal struct .p.
p.y = 2; // simpler .
// WOW! NO ERRORS! but looks stupid
I can see error if I try to initialize one field twice, I don't see another errors in case (1)
assign list or initialization with list should be simpler (that means faster parser)
Comment #3 by robert.schadek — 2024-12-13T18:59:51Z