Re: First-class callable partial application
| From: | Larry Garfield | Date: | Fri, 10 Mar 2023 19:52:57 +0000 |
| Subject: | Re: First-class callable partial application | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-119680@lists.php.net to get a copy of this message | ||
On Fri, Mar 10, 2023, at 1:04 PM, Eugene Sidelnyk wrote:
> Hello internals!
> I'm just wondering why First class callable syntax doesn't allow partial
> application?
> Recently I stumbled across following scenario where it could be really
> useful:
>
> ```
> public function __invoke(SendOtpCommand $command)
> {
> $this->cache->get($command->getPhone(), $this->storeOtp($command,
> ...));
> }
>
> private function storeOtp(SendOtpCommand $command, ItemInterface $item)
> {
> // some logic
> }
> ```
>
> In this example, I supposed that the closure created will accept a single
> parameter $item, and when it is called back, method storeOtp will accept
> both $command and $item. As it turned out to be, it doesn't really work
> this way.
>
> Another simplified example:
>
> ```
> // partial application
> foo(bar(1, ...));
>
> function foo(Closure $closure)
> {
> $closure(2);
> }
>
> function bar(int $a, int $b)
> {
> var_dump($a, $b); // 1, 2
> }
> ```
>
> Closure in foo accepts only one parameter. But when it is actually
> dispatched, bar is called with two arguments.
>
> Are there any pitfalls, which prevent implementation of this nifty feature?
There actually was an RFC for full partial application a few years ago:
https://wiki.php.net/rfc/partial_function_application
The main drawback is that in order to work, it had to do some very tricksy things with very critical
parts of the code (how functions get called). Enough people felt that was too risky for the
functionality and it didn't pass. First-class-callables were deliberately submitted
(https://wiki.php.net/rfc/first_class_callable_syntax) as a sort of "junior version" of
partial function application that could be done with far less invasive changes.
I'd *love* to see some form of PFA make a come-back, either with a simpler implementation or a
reduced scope that allows for a simpler implementation. I am not aware of anyone actively working
on it at the moment, though. (If anyone is, please speak up!)
--Larry Garfield