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

This commit is contained in:
Andrei Alexandrescu 2020-09-15 12:00:25 -04:00 committed by The Dlang Bot
parent 9351e91fe4
commit cae6379750

View file

@ -530,6 +530,7 @@ Returns:
*/ */
auto clamp(T1, T2, T3)(T1 val, T2 lower, T3 upper) auto clamp(T1, T2, T3)(T1 val, T2 lower, T3 upper)
if (is(typeof(max(min(val, upper), lower))))
in in
{ {
import std.functional : greaterThan; import std.functional : greaterThan;
@ -537,7 +538,7 @@ in
} }
do do
{ {
return max(lower, min(upper, val)); return max(min(val, upper), lower);
} }
/// ///
@ -576,6 +577,14 @@ do
// UFCS style // UFCS style
assert(Date(1982, 1, 4).clamp(Date.min, Date.max) == Date(1982, 1, 4)); assert(Date(1982, 1, 4).clamp(Date.min, Date.max) == Date(1982, 1, 4));
// Stability
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);
} }
// cmp // cmp