Bug 8363 – Empty array literal does not implicitly convert to associative array

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-07-09T06:22:08Z
Last change time
2018-05-05T12:53:06Z
Assigned to
No Owner
Creator
Puneet Goel

Comments

Comment #0 by puneet — 2012-07-09T06:22:08Z
Deleting all the entries from an associative array is not intuitive. This has been discussed at the D forum here: http://forum.dlang.org/thread/[email protected] I suggest the following syntax should work int[string] aa; aa["foo"] = 1; aa = []; // clear all array entries At this point only this works. I think this is not very intuitive. int[string] aa; int[string] empty; aa["foo"] = 1; aa = empty;
Comment #1 by timon.gehr — 2012-07-09T06:49:35Z
aa.clear() should work.
Comment #2 by puneet — 2012-07-09T07:11:51Z
(In reply to comment #1) > aa.clear() should work. I had not tried it because the forum topic (only one year old) said that it does not worl. But it does seem too work now. Can we add that to the Assoc array documentation here http://dlang.org/hash-map.html ?
Comment #3 by yebblies — 2012-07-09T08:54:12Z
(In reply to comment #0) > aa = []; // clear all array entries > This should probably work. > > At this point only this works. I think this is not very intuitive. > > int[string] aa; > int[string] empty; > > aa["foo"] = 1; > aa = empty; try aa = null; But be careful, don't forget AAs are reference types.
Comment #4 by issues.dlang — 2012-07-09T10:45:46Z
> aa.clear() should work. I believe that that's the same as setting it to null. It doesn't actually remove anything from the AA. It just makes that reference null. If you want to actually remove all of the elements from the AA, you currently have to iterate over them and call remove on each one.
Comment #5 by issues.dlang — 2012-07-09T11:02:47Z
However, with clear being renamed to destroy for 2.060 (with clear scheduled for deprecation), I don't see any problem with adding a clear function to AAs which actually clears the AA (that's probably what most everyone using clear on AAs wanted anyway). Presumably, that could be done as part of the AA implementation redesign that's currently being worked on.
Comment #6 by oli_r — 2018-05-05T12:53:06Z
Starting with 2.071, you can use "aa.clear()" to achieve the desired behaviour. If you are still in the need for the proposed syntax, please reopen this issue or write a DIP (as this would modify the behaviour of the language).