On Thu, Jun 17, 2021, at 9:45 AM, Côme Chilliet wrote: > Le Thu, 17 Jun 2021 08:37:23 -0500, > "Larry Garfield" <la...@garfieldtech.com> a écrit : > > > On Thu, Jun 17, 2021, at 2:54 AM, Côme Chilliet wrote: > > > > $c = stuff(...); > > > > $c = fn(int $i, string $s, float $f, Point $p, int $m = 0) > > > > => stuff($i, $s, $f, $p, $m); > > > > > > > $c = stuff(1, 'hi', 3.4, $point, 5, ...); > > > > $c = fn(...$args) => stuff(1, 'hi', 3.4, $point, 5, ...$args); > > > > > > Why is there an additional variadic parameter in this one? > > > > ... means "zero or more". In this case, it means zero, that is, it creates > > a > > closure that requires no arguments and will call the original function with > > all of the provided values later. This is the "deferred function" use case > > mentioned further down. > > I still do not understand why there is an added variadic parameter when > using ... in stuff(1, 'hi', 3.4, $point, 5, ...); but not when using it in > stuff(...); > What happens when we use stuff(1, 'hi', 3.4, $point, ...); ? > > > > Also, in the second set of examples: > > > > function things(int $i, float $f, Point ...$points) { ... } > > > > > > > // Ex 13 > > > > $c = things(...); > > > > $c = fn(int $i, float $f, ...$args) => things(...[$i, $f, ...$args]); > > > > > > > // Ex 14 > > > > $c = things(1, 3.14, ...); > > > > $c = fn(...$args) => things(...[1, 3.14, ...$args]); > > > > > > What happens to the typing of the variadic parameter here? Why is it > > > removed? > > > > > > It would feel natural that the ... means "copy the rest of the parameters > > > from signature". Here it seems it sometimes mean that, and sometimes mean > > > "accept an additional variadic parameter and pass it along". > > > > Internally placeholders do mean the former. A trailing variadic, though, > > can > > accept extra arguments of potentially not pre-defined types, so it sort of > > straddles the line. Variadics make things weird. :-) (Dating from PHP > > 5.6.) In the majority case, though, thinking of them as "copy the rest of > > the arguments" is accurate. > > I do not understand why Points ...$points becomes untyped ...$args when using > things(...), while when using stuff(...) earlier no typing was lost.
The type information is not lost. The examples are not exact equivalents, but approximate equivalents. Joe confirmed that the closure that's generated really does retain the type information: krakjoe@Fiji:/opt/src/php-src$ cat wat.php <?php function things(int $i, float $f, Point ...$points) { } $c = things(...); echo (string) new ReflectionFunction($c); krakjoe@Fiji:/opt/src/php-src$ sapi/cli/php wat.php Function [ <user> partial function things ] { @@ /opt/src/php-src/wat.php 4 - 4 - Parameters [3] { Parameter #0 [ <optional> int $i ] Parameter #1 [ <optional> float $f ] Parameter #2 [ <optional> Point ...$points ] } } --Larry Garfield -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php