Bug 20617 – There is no support for copying hashmaps in Druntime / Phobos

Status
NEW
Severity
enhancement
Priority
P4
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-02-27T01:30:35Z
Last change time
2024-12-07T13:39:58Z
Assigned to
No Owner
Creator
Andrej Mitrovic
Moved to GitHub: dmd#17397 →

Comments

Comment #0 by andrej.mitrovich — 2020-02-27T01:30:35Z
----- void main() { int[int] map1 = [1:1, 2:2, 3:3]; int[int] map2; map2.copy(map1); // ? } ----- I am not asking for special syntax. Stuff like 'map2 = map1[]` would look cool, but it's outside the scope here. There should be `copy` functionality implemented somewhere in Druntime / Phobos, it's not ideal that we have to write this code manually such as: ----- map1.byKeyValue.each!(pair => map2[pair.key] = pair.value); -----
Comment #1 by andrej.mitrovich — 2022-07-04T17:20:59Z
Current workaround: ----- import std.array; import std.algorithm; import std.typecons; void main() { int[int] map1 = [1:1, 2:2, 3:3]; int[int] map2 = assocArray(map1.byKeyValue().map!(k => tuple(k.key, k.value))); } ----- Notice that byKeyValue() does not return tuples, instead it returns 'Pair(...)' structs, so these can't directly be passed to `assocArray`. I suppose we could add an overload of `assocArray` that takes in `Pair()`s as arguments, but that seems a bit "leaky" abstractions to me. I'd rather we add an explicit 'duplicate' functionality to Druntime.
Comment #2 by andrej.mitrovich — 2022-07-05T03:46:58Z
Never mind, I found a better way: ----- import std.array; void main() { int[int] map1 = [1:1, 2:2, 3:3]; int[int] map2 = assocArray(map1.byPair()); } ----- Perhaps this should be added to the docs if it's not already there.
Comment #3 by robert.schadek — 2024-12-07T13:39:58Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17397 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB