Bug 19115 – Object and Collection Initializers

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-07-24T09:18:56Z
Last change time
2024-12-13T18:59:51Z
Assigned to
No Owner
Creator
a11e99z
Moved to GitHub: dmd#17871 →

Comments

Comment #0 by black80 — 2018-07-24T09:18:56Z
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
Related struct initialization DIP[0] and its discussion[1]. We should probably wait for this DIP or "tack" support for classes on. [0] https://github.com/dlang/DIPs/pull/71 [1] https://forum.dlang.org/post/[email protected]
Comment #2 by black80 — 2018-07-24T09:58:54Z
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
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17871 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB