Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115054 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 86097 invoked from network); 23 Jun 2021 08:13:39 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Jun 2021 08:13:39 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C58EA180507 for ; Wed, 23 Jun 2021 01:32:07 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.4 required=5.0 tests=BAYES_20,KHOP_HELO_FCRDNS, SPF_HELO_NONE,SPF_NONE,UNPARSEABLE_RELAY autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from processus.org (ns366368.ip-94-23-14.eu [94.23.14.201]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 23 Jun 2021 01:32:07 -0700 (PDT) Received: from authenticated-user (PRIMARY_HOSTNAME [PUBLIC_IP]) by processus.org (Postfix) with ESMTPA id 114095101324 for ; Wed, 23 Jun 2021 08:32:04 +0000 (UTC) To: internals@lists.php.net References: <1014AE76-B3D5-48F7-A0F0-B8E3327123A2@gmail.com> Message-ID: Date: Wed, 23 Jun 2021 10:32:03 +0200 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US Authentication-Results: processus.org; auth=pass smtp.auth=pierre-php@processus.org smtp.mailfrom=pierre-php@processus.org X-Spamd-Bar: / Subject: Re: [PHP-DEV] Proposal: clamp From: pierre-php@processus.org (Pierre) Le 23/06/2021 à 10:28, Lynn a écrit : > On Wed, Jun 23, 2021 at 3:07 AM Marco Pivetta wrote: > >> The problem is exactly the fact that it is trivial to implement in >> userland: why not do it there instead? >> > My 2cents: because people won't use it when the barrier is too high to get > it. There are a ton of great libraries that have functionality like clamp, > and some more elaborate, like an array replacement. In the end -based on my > experience- people go to the php documentation and look for functions that > they can use, or they use their IDE autocomplete functionality to see > what's hinted at. Having these functions spread through the ecosystem means > discoverability is low. I've needed the "clamp" function in the past and > didn't even consider looking for a library to do this for me. Why would I > even install a library to have a single function anyway? It feels like a > massive overhead. I've seen coworkers use funky SPL functionality I didn't > even know existed to avoid using external dependencies that have already > solved their problem in a neat way, which were in fact already installed > through composer. They found the solution through google and ended up in > the official php documentation. > > I'd love to see these small additions in the core, they make the language > feel more complete. The fact that they are easy to implement in userland > means that we can have great forward compatibility through polyfills and > already start using them in codebases that haven't been upgraded yet to the > latest PHP version. I've been very happy to have been able to use > str_starts_with, str_ends_with, and str_contains in 7.3, as they came in > with a generic php polyfill. > > With a "see also: clamp" in the min/max function documentation, this would > be easy to find as well. > +1 for this, I often use the min/max pattern to achieve the same result, having a single method would be much more readable. Regards, -- Pierre