Skip to content

Commit 91308f2

Browse files
authored
Merge pull request #8717 from burner/clamp_static_assert_message
Better std.algorithm.comparison.clamp error messages
2 parents 1171a1f + d8c627e commit 91308f2

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

changelog/clamp_assert_message.dd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Better static assert messages for `std.algorithm.comparison.clamp`
2+
3+
Until now, `clamp` used a template constraint to check if the passed types
4+
could be used. If they were not, it was very tedious to figure out why.
5+
6+
As the template constraint is not used for overload resolution
7+
the constrains are moved into static asserts with expressive error
8+
messages.

std/algorithm/comparison.d

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -577,19 +577,20 @@ Returns:
577577
and `T3` are different.
578578
*/
579579
T1 clamp(T1, T2, T3)(T1 val, T2 lower, T3 upper)
580-
if (is(typeof(val.lessThan(lower) ? lower : val.greaterThan(upper) ? upper : val))
581-
&& (is(T2 : T1) && is(T3 : T1)))
582-
// cannot use :
583-
// `if (is(typeof(val.lessThan(lower) ? lower : val.greaterThan(upper) ? upper : val) : T1))
584-
// because of https://issues.dlang.org/show_bug.cgi?id=16235.
585-
// Once that is fixed, we can simply use the ternary in both the template constraint
586-
// and the template body
587-
in
588580
{
581+
static assert(is(T2 : T1), "T2 of type '", T2.stringof
582+
, "' must be implicitly convertible to type of T1 '"
583+
, T1.stringof, "'");
584+
static assert(is(T3 : T1), "T3 of type '", T3.stringof
585+
, "' must be implicitly convertible to type of T1 '"
586+
, T1.stringof, "'");
587+
589588
assert(!lower.greaterThan(upper), "Lower can't be greater than upper.");
590-
}
591-
do
592-
{
589+
590+
// `if (is(typeof(val.lessThan(lower) ? lower : val.greaterThan(upper) ? upper : val) : T1))
591+
// because of https://issues.dlang.org/show_bug.cgi?id=16235.
592+
// Once that is fixed, we can simply use the ternary in both the template constraint
593+
// and the template body
593594
if (val.lessThan(lower))
594595
return lower;
595596
else if (val.greaterThan(upper))

0 commit comments

Comments
 (0)