D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 21249 - clamp() is not stable and is not constrained
Summary: clamp() is not stable and is not constrained
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2020-09-15 15:56 UTC by Andrei Alexandrescu
Modified: 2020-09-15 23:20 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrei Alexandrescu 2020-09-15 15:56:42 UTC
For elaborate objects defining comparison, clamp() should return the leftmost object unless it falls outside the limits established by the other two parameters.

Consider:

    struct A {
        int x, y;
        int opCmp(ref const A rhs) const { return (x > rhs.x) - (x < rhs.x); }
    }
    A x, lo, hi;
    x.y = 42;
    assert(x.clamp(lo, hi).y == 42);

Also, clamp() needs to be constrained appropriately.
Comment 1 Dlang Bot 2020-09-15 16:01:05 UTC
@andralex created dlang/phobos pull request #7634 "Fix Issue 21249 - clamp() is not stable and is not constrained" fixing this issue:

- Fix Issue 21249 - clamp() is not stable and is not constrained

https://github.com/dlang/phobos/pull/7634
Comment 2 Dlang Bot 2020-09-15 23:20:57 UTC
dlang/phobos pull request #7634 "Fix Issue 21249 - clamp() is not stable and is not constrained" was merged into master:

- a7d574bcba1e0104938c203615339f4fb081ed7b by Andrei Alexandrescu:
  Fix Issue 21249 - clamp() is not stable and is not constrained

https://github.com/dlang/phobos/pull/7634