[Issue 10535] [AA] Add a function to druntime which returns an empty AA

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Feb 27 17:23:25 UTC 2019


https://issues.dlang.org/show_bug.cgi?id=10535

Nick Treleaven <nick at geany.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nick at geany.org
           See Also|                            |https://issues.dlang.org/sh
                   |                            |ow_bug.cgi?id=16269

--- Comment #4 from Nick Treleaven <nick at geany.org> ---
(In reply to Daniel Kozak from comment #3)
> auto aa = new VT[KT]();

This would be good (although I don't think the () parens serve a purpose). `new
AA` is natural syntax for allocating an empty AA, unlike []. Having `new` in
the syntax is a great reminder to the programmer that they're allocating memory
even though the AA is empty.

V[K][6] sixAAs = new V[K];
V[K][6] sixAAs = [];

The `new` version above may remind the programmer that each AA reference will
point to the same empty heap AA. The `[]` version looks like initializing each
AA reference to an empty AA, which isn't clear that the AA references all point
to the same memory. In this case the programmer may well have intended each AA
reference to point to a unique empty heap AA, so that inserting an element in
one isn't reflected into every AA reference.

(In reply to monarchdodra from comment #1)
> There might be a better "generic" solution to the "empty initialization"
> (eg: "arg-less-constructor") problem.

For user-defined reference types, the solution is the zero-arg class
constructor, `new Container`. That mirrors the `new V[K]` syntax.

--


More information about the Digitalmars-d-bugs mailing list