Re: First-class callable partial application
| From: | Rowan Tommins | Date: | Tue, 14 Mar 2023 12:56:50 +0000 |
| Subject: | Re: First-class callable partial application | ||
| References: | 1 2 3 4 5 6 7 8 9 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-119692@lists.php.net to get a copy of this message | ||
On Tue, 14 Mar 2023 at 10:39, Bob Weinand <bobwei9@hotmail.com> wrote:
> Hey Rowan,
>
> do we actually need *positional* partial application, after a ... token?
>
> Would it not be enough, to simply forbid positional arguments after a ...
> and just allow named arguments? These already have well defined position
> independent semantics.
>
> There may be some desire for a single argument placeholder later on, but
> this can be introduced later, separately.
>
Yes, named parameters would certainly be better than left-to-right only.
It's definitely less elegant, though, and given that PFA is largely
short-hand for a short closure anyway, I think conciseness is quite an
important aim.
To take a couple of the above examples, and compare existing short closure,
fully positional PFA, and named-after-placeholder PFA:
$isLogger = fn($object) => is_subclass_of($object, LoggerInterface::class,
false);
$isLogger = is_subclass_of(?, LoggerInterface::class, false);
$isLogger = is_subclass_of(..., class: LoggerInterface::class,
allow_string: false);
$priceFormatter = fn(float $num) => number_format($num, 2, ',', '.');
$priceFormatter = number_format(?, 2, ',', '.');
$priceFormatter = number_format(..., decimals: 2, decimal_separator: ',',
thousands_separator: '.');
Arguably the named param version is more explicit, but in some cases it's
significantly longer than manually defining a closure, whereas fully
positional PFA is always shorter.
Regards,
--
Rowan Tommins
[IMSoP]