Re: [PHP-DEV] [Discussion] Revisiting typed closures

[email protected] ("Rowan Tommins [IMSoP]")
Newsgroups php.internals
Message-ID <[email protected]>
On 1 July 2026 20:02:27 BST, Seifeddine Gmati <[email protected]> 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]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.