Re: [RFC] Add clamp function
| From: | Larry Garfield | Date: | Fri, 22 Aug 2025 20:09:11 +0000 |
| Subject: | Re: [RFC] Add clamp function | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-128534@lists.php.net to get a copy of this message | ||
On Fri, Aug 22, 2025, at 12:45 PM, Kyle Katarn wrote:
> About "What determines comparability", it follows the usual rules of
> PHP:
> https://www.php.net/manual/en/language.operators.comparison.php
>
> So it's equivalent to ($value < $min) ? $min : (($value > $max) ? $max
> : $value) and also equivalent to min($max, max($min, $value))
>
> About clamp(new Point(1, 2), new Point(0, 0), new Point(5, 5));
>
> If Point is a comparable value (simple DTO for example), it should
> return $value, like when doing ($value < $min) ? $min : (($value >
> $max) ? $max : $value) we could add a test for such case, but I think
> that for consistency, whatever currently works in min() should work in
> clamp()
>
> Following the link of the implementation, there is also a link to the
> documentation where I already explained the comparison rules following
> the example of what was done in the documentation for min() and max():
> https://github.com/php/doc-en/pull/4814
Please make sure the above is captured in the RFC.
Though apparently what PHP currently does with Point comparisons in min/max is... weird. I
don't even know what the logic here is. :)
https://3v4l.org/pTmiV
(No need to change it in this RFC, just note explicitly that the expected behavior is identical to
that min(max()) construct, regardless of type.)
--Larry Garfield