Bug 7389 – Disallow or implement correct SortedRange equality
Status
RESOLVED
Resolution
WORKSFORME
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-01-28T06:10:00Z
Last change time
2014-02-16T14:27:56Z
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2012-01-28T06:10:59Z
Given two small unsorted arrays 'a1' and 'a2', a common natural idiom to test that they have the same items is:
a1.sort() == a2.sort()
But currently in D2 it's a trap:
import std.algorithm: sort;
void main() {
auto a1 = [1, 2];
auto a2 = [2, 1];
assert(a1.sort() == a2.sort()); // AssertError
assert(a1.sort().release() == a2.sort().release()); // OK
}
To avoid such bugs I suggest to statically forbid the == operation among two SortedRange; or _better_ to implement it correctly, and allow that testing idiom.
Comment #1 by bearophile_hugs — 2012-01-28T06:19:26Z
Another workaround:
import std.algorithm: sort, equal;
void main() {
auto a1 = [1, 2];
auto a2 = [2, 1];
assert(equal(a1.sort(), a2.sort())); // OK
}
Comment #2 by peter.alexander.au — 2014-01-26T08:46:45Z