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