D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6384 - std.traits.isComparable
Summary: std.traits.isComparable
Status: RESOLVED FIXED
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: 2011-07-26 07:38 UTC by bearophile_hugs
Modified: 2018-01-05 13:30 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 2011-07-26 07:38:21 UTC
I suggest to add isComparable to std.traits, to be used as template constraint for (both user defined and standard library) sorting/searching functions. It looks for a opCmp or in general for


template isComparable(T) {
    enum bool isComparable = is(typeof({ return T.init < T.init; }));
}

version (unittest) {
    static assert(isComparable!int);
    static assert(isComparable!string);
    static assert(!isComparable!creal);

    static struct Foo {}
    static assert(!isComparable!Foo);

    static struct Bar {
        bool opCmp(Bar) { return true; }
    }
    static assert(isComparable!Bar);
}


I think using this constraint will improve the error messages of functions like std.algorithm.sort too, etc:

import std.algorithm;
static struct Foo {}
static struct Bar {
    bool opCmp(Bar) { return true; }
}
void main() {
    auto a1 = [Foo(), Foo()];
    sort(a1);
    auto a2 = [Bar(), Bar()];
    sort(a2);
}


With DMD 2.054 it gives:

...\dmd\src\phobos\std\algorithm.d(6438): Error: static assert  "Invalid predicate passed to sort: a < b"
test.d(8):        instantiated from here: sort!("a < b",cast(SwapStrategy)0,Foo[])
Comment 1 kennytm 2011-07-26 08:40:51 UTC
Note that `std.algorithm.sort` can accept arbitrary predicate, and you can use something like


    Complex!double[] cmplxarr;
    ...
    sort!"a.re < b.re"(cmplxarr);


If the purpose is to improve the error message of `std.algorithm.sort`, `isComparable` won't work.
Comment 2 hsteoh 2013-08-22 20:16:25 UTC
There are actually at least two possible isComparable's that would be helpful in signature constraints. One checks if "a<b" compiles, the other checks if "a==b" compiles.

I notice that some Phobos code uses is(typeof(a) : typeof(b)) in signature constraints, only to write if(a==b) in the function body, which is fallacious: just because a is implicitly convertible to b does not necessarily mean == is defined between them.

More generally, code should really test for specific operations they need to use, instead of just assuming that is(A : B) implies A.init <op> B.init is valid. To that end, maybe we need isValidBinaryOp instead:

template isValidBinaryOp(T1, string op, T2) {
    alias isValidBinaryOp = is(mixin("T1.init" ~ op ~ "T2.init") : bool);
}

then we could write signature constraints of the form:

auto myFunc(T,U)(T t, U u)
    if (isValidBinaryOp!(T, "<", U))
{ ... }
Comment 3 github-bugzilla 2017-07-31 15:00:45 UTC
Commits pushed to master at https://github.com/dlang/phobos

https://github.com/dlang/phobos/commit/24bfb294b7523307d10aaaf41ad2021c6cb38501
Fix Issue 6384 - add std.traits.isComparable

https://github.com/dlang/phobos/commit/a6ef870042c2c569c31173645f01a2ec682fc283
Merge pull request #5566 from RazvanN7/Issue_6384

Fix Issue 6384 - add std.traits.isComparable
merged-on-behalf-of: Andrei Alexandrescu <andralex@users.noreply.github.com>
Comment 4 github-bugzilla 2017-08-16 13:24:20 UTC
Commits pushed to stable at https://github.com/dlang/phobos

https://github.com/dlang/phobos/commit/24bfb294b7523307d10aaaf41ad2021c6cb38501
Fix Issue 6384 - add std.traits.isComparable

https://github.com/dlang/phobos/commit/a6ef870042c2c569c31173645f01a2ec682fc283
Merge pull request #5566 from RazvanN7/Issue_6384
Comment 5 github-bugzilla 2018-01-05 13:30:42 UTC
Commits pushed to dmd-cxx at https://github.com/dlang/phobos

https://github.com/dlang/phobos/commit/24bfb294b7523307d10aaaf41ad2021c6cb38501
Fix Issue 6384 - add std.traits.isComparable

https://github.com/dlang/phobos/commit/a6ef870042c2c569c31173645f01a2ec682fc283
Merge pull request #5566 from RazvanN7/Issue_6384