www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21249] New: clamp() is not stable and is not constrained


          Issue ID: 21249
           Summary: clamp() is not stable and is not constrained
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: andrei erdani.com

For elaborate objects defining comparison, clamp() should return the leftmost
object unless it falls outside the limits established by the other two


    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.

Sep 15 2020