Re: [RFC] [Discussion] Add clamp function
| From: | Hans Krentel | Date: | Thu, 28 Aug 2025 06:35:08 +0000 |
| Subject: | Re: [RFC] [Discussion] Add clamp function | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-128577@lists.php.net to get a copy of this message | ||
On Wednesday 27 August 2025 17:45:59 (+02:00), Marco Pivetta wrote:
I know this isn't very used in internals, but does it make sense to write
generics for the generated stubs of this function? [...] /** * @template T * @param T $value * @param T $min * @param T $max * @return T */ function clamp (mixed $value, mixed $min, mixed $max): mixed {+1 This is an excellent suggestion. Using a template ensures type safety and clearly communicates the intended relationship between the arguments and return value, which is a significant improvement. This also highlights a minor but important clarification for the documentation. To make the behavior perfectly unambiguous, I suggest a small edit to explicitly state the bounds are inclusive. Current text:
Suggested edit:clamptakes three arguments, a$value,$minand$max, then checks if$valueis within the bounds of$minand$max.
This is a minimal change that adds crucial clarity already in the first sentence. --hakreclamptakes three arguments, a$value,$minand$max, then checks if$valueis within the bounds of$minand$max(both inclusive).