D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7389 - Disallow or implement correct SortedRange equality
Summary: Disallow or implement correct SortedRange equality
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-01-28 06:10 UTC by bearophile_hugs
Modified: 2014-02-16 14:27 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2012-01-28 06:10:59 UTC
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 bearophile_hugs 2012-01-28 06:19:26 UTC
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 Peter Alexander 2014-01-26 08:46:45 UTC
This appears to have been fixed in 2.063

http://dlang.org/changelog.html#structuralcompare
Comment 3 Peter Alexander 2014-02-16 14:27:56 UTC
Changed to WORKSFORME so that it doesn't appear in the changelog for next update.