Bug 12029 – Swap on std.container.Array?

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-01-29T08:26:22Z
Last change time
2020-03-21T03:56:41Z
Assigned to
No Owner
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2014-01-29T08:26:22Z
I'd like to use std.algorithm.swap on the items of std.array.Array. Currently it's not possible, as shown here: void main() { import std.container: Array; import std.range: hasSwappableElements; auto arr = Array!int([1, 2]); pragma(msg, hasSwappableElements!(typeof(arr))); } With dmd 2.065beta it outputs at compile time: false Current workaround is to swap the items manually: void main() { import std.container: Array; auto arr = Array!int([1, 2]); // Swap: auto aux = arr[0]; arr[0] = arr[1]; arr[1] = aux; assert(arr[0] == 2 && arr[1] == 1); } If std.container.Array was designed this way on purpose, perhaps to keep its memory tightly sealed inside the container itself, then close down this enhancement request.
Comment #1 by peter.alexander.au — 2014-01-29T11:57:12Z
They should be swappable once the containers are unsealed. https://github.com/D-Programming-Language/phobos/pull/1857
Comment #2 by justin — 2014-06-06T17:44:40Z
This still fails in git head.
Comment #3 by b2.temp — 2015-11-21T12:54:43Z
swap() now works on Array: _______________ void main() { import std.algorithm; import std.container: Array; auto arr = Array!int([1, 2]); swap(arr[0], arr[1]); assert(arr[0] == 2 && arr[1] == 1); } _______________