Re: [Discussion] Revisiting typed closures
| From: | Rowan Tommins [IMSoP] | Date: | Wed, 01 Jul 2026 22:02:46 +0000 |
| Subject: | Re: [Discussion] Revisiting typed closures | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-131686@lists.php.net to get a copy of this message | ||
On 1 July 2026 20:02:27 BST, Seifeddine Gmati <azjezz@carthage.software> wrote:
>Making
Closure generic (e.g., say
>Closure<Input, Output> ) would
>require Input to be variadic to stand in for a closure's
>parameter
>list, and variadic generics are a different beast entirely.
FWIW, C# has a long list of overloads for expressing generic lambda types, like Action<T1,T2>,
Action<T1,T2,T3>, Func<T1,T2,TResult> and so on and on and on <https://github.com/dotnet/dotnet/blob/b0f34d51fccc69fd334253924abd8d6853fad7aa/src/runtime/src/libraries/System.Private.CoreLib/src/System/Action.cs>
<https://github.com/dotnet/dotnet/blob/b0f34d51fccc69fd334253924abd8d6853fad7aa/src/runtime/src/libraries/System.Private.CoreLib/src/System/Function.cs>
Ugly, and unlikely to even work in a PHP implementation of generics.
On the other hand, it has an elegant "delegate" syntax for what are effectively named
callable types: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/
That *would* translate well to PHP. Translating one of their examples:
delegate ProcessBookCallback(Book $book): void;
public function processPaperbackBooks(ProcessBookCallback $processBook): void {
foreach ($this->list as $b) {
if ($b->paperback) {
$processBook($b);
}
}
}
Just an additional angle to throw into the mix.
Rowan Tommins
[IMSoP]