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

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-07-03T22:18:30Z
Last change time
2022-07-27T13:16:46Z
Keywords
pull
Assigned to
No Owner
Creator
Jonathan M Davis
See also
https://issues.dlang.org/show_bug.cgi?id=16269, https://issues.dlang.org/show_bug.cgi?id=7227

Comments

Comment #0 by issues.dlang — 2013-07-03T22:18:30Z
Right now, the only way to get an empty AA (as opposed to a null one) is to add an element to it and then remove it, which is neither user-friendly, nor efficient. It would be easy enough to create a function which at least wrapped all that (creating the AA, adding the element, and then removing it), but it would be more efficient if the AA implementation did it, since it should be able to do it without having to add or remove an element. It would also be possible for the AA implementation to do it without using the init property (as adding and then removing an element would likely require the init property in order to get an element to add). So, this enhancement request is for adding a function to druntime which returns an empty AA without actually adding or removing elements from the AA to do it.
Comment #1 by monarchdodra — 2013-07-03T22:40:22Z
Not to hijack this, but before we add that AA specific function, we should take into account that a LOT of other objects require this. Basically, every reference semantic object in Phobos: Containers, Appender, PRNGs... There might be a better "generic" solution to the "empty initialization" (eg: "arg-less-constructor") problem.
Comment #2 by hsteoh — 2013-08-17T13:11:48Z
(In reply to comment #1) > Not to hijack this, but before we add that AA specific function, we should take > into account that a LOT of other objects require this. Basically, every > reference semantic object in Phobos: Containers, Appender, PRNGs... > > There might be a better "generic" solution to the "empty initialization" (eg: > "arg-less-constructor") problem. This does not preclude adding a function to aaA.d that returns an empty but non-null AA, since that will be needed anyway no matter what generic solution is provided to the end-user.
Comment #3 by kozzi11 — 2014-08-06T14:55:44Z
What about allow this: auto aa = new VT[KT](); or VT[KT] aa = []; http://forum.dlang.org/thread/[email protected]#post-sfcppaolmnhxfjvrekkm:40forum.dlang.org
Comment #4 by nick — 2019-02-27T17:23:25Z
(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.
Comment #5 by dlang-bot — 2019-03-02T17:11:27Z
@ntrel created dlang/druntime pull request #2504 "Fix Issue 10535 - Add initialize function to allocate an empty AA" fixing this issue: - Fix Issue 10535 - Add AA initialize function https://github.com/dlang/druntime/pull/2504
Comment #6 by nick — 2019-03-02T17:21:51Z
*** Issue 16269 has been marked as a duplicate of this issue. ***
Comment #7 by nick — 2022-07-27T13:16:46Z
dlang/dmd pull request #14257 "Make `new AA` allocate the associative array Impl" was merged into master: - e8cdd13a92487ad6ef206a9ca2e8c030a6c56e68 by Nick Treleaven: Fix 2 fail_compilation tests https://github.com/dlang/dmd/pull/14257