Re: RFC idea: using the void type to control maximum arity of user-defined functions
| From: | Pablo Rauzy | Date: | Mon, 08 Apr 2024 12:00:57 +0000 |
| Subject: | Re: RFC idea: using the void type to control maximum arity of user-defined functions | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-123041@lists.php.net to get a copy of this message | ||
Hello all,
Le 05/04/2024 à 15:53, Larry Garfield a écrit :
On Thu, Apr 4, 2024, at 9:27 PM, Ilija Tovilo wrote:So what should be done to move forward with this? Should the old RFC on strict argument count be revived? Or should a new RFC proposal be written? If so, should it contain an approval voting (where voters can select any number of candidates), prior to the RFC proposal vote itself, to decide if the change should be: strict argument count, using the void keyword to explicitely stop the argument list, or using a #[Nonvariadic] attribute? Best regards, -- PabloOn Thu, Apr 4, 2024 at 5:58 PM Tim Düsterhus<tim@bastelstu.be> wrote:This is unfortunately not reliable today, because of the difference between how internal functions and user-defined ones are handled. The code above will fatal if you use a callable that is defined in stdlib rather than in user-space. I have been bitten by this many times, and is why I ended up with double the functions in my FP library: cf:https://github.com/Crell/fp/blob/master/src/array.php#L34 It's also been argued to me rather effectively that ignoring trailing values and optional arguments creates a whole bunch of exciting landmines, as you may pass an "extra" parameter to a function expecting it to be ignored, but it's actually part of the optional arguments so gets used. The standard example here is intval($value, $base=10). Basically no one uses the $base parameter, and most people forget it exists, but if you allow an optional value to get passed to that, especially if in weak typing mode, you could get hilariously wrong results. (For sufficiently buggy definitions of hilarious.) The behavior difference between internal and user-defined functions is the root issue. One way or another it should be addressed, because the current behavior is a landmine I have stepped on many times. --Larry GarfieldOn 4/4/24 16:36, Pablo Rauzy wrote:IIRC one of the bigger downsides of this change are closure calls that may provide arguments that the callee does not care about. https://3v4l.org/0QdoSI strongly agree in theory, but this could break existing code, and moreover such a proposal was already rejected: https://wiki.php.net/rfc/strict_argcountThe RFC is 9 years old by now. My gut feeling is be that using an actual variadic parameter for functions that are variadic is what people do, because it makes the function signature much clearer. Actually variadic parameters are available since PHP 5.6, which at the time of the previous RFC was the newest version. Since then we had two major releases, one of which (7.x) is already out of support. I think it would be reasonable to consider deprecating passing extra arguments to a non-variadic function.function filter($array, callable $c) { $result = []; foreach ($array as $key => $value) { if ($c($value, $key)) { $result[$key] = $value; } } return $result; } var_dump(filter(['foo', '', 'bar'], function ($value) { return strlen($value); })); // Internal functions already throw on superfluous args var_dump(filter(['foo', '', 'bar'], 'strlen'));The user may currently choose to omit the $key parameter of the closure, as it is never used. In the future, this would throw. We may decide to create an exemption for such calls, but I'm not sure replacing one inconsistency with another is a good choice. Ilija