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

[email protected] (Seifeddine Gmati)
Newsgroups php.internals
Message-ID <CAFyg4HitcEk7=dQkedbD9MeB3AUU3XLnwfJ5gREQWG98hDwALQ@mail.gmail.com>
On Wed, 1 Jul 2026 at 15:43, Matheus Martins <[email protected]> wrote:
>
> Hello internals,
>
> I would like to revisit the idea of giving closures a typed call signature.
>
> e.g. Closure(int, string): array -- enforced at the point a value crosses a
> type boundary (an argument, a return, a property), the same places any other
> type is checked.
>
> Today a closure can only be typed as Closure or callable, neither of which
> says anything about its parameters or return, even though that information is
> right there.
>
> I know this is not new ground: Callable Prototypes was declined in 2016, and
> Garfield and Grekas shared two further RFCs in 2023 -- Structural Typing for
> Closures, and Allow Closures to Declare Interfaces they Implement -- both still
> in draft.
>
> Before taking it further, I would like to know whether closure typing
> is still considered worth pursuing -- or whether the topic is now regarded as
> settled.
>
> References:
>
> - https://wiki.php.net/rfc/callable-types
> - https://wiki.php.net/rfc/structural-typing-for-closures
> - https://wiki.php.net/rfc/allow-closures-to-declare-interfaces-they-implement
>
> Thanks.

Hi Matheus,

I'm in favor of adding this. However, syntax wise, i would rather we
re-use the `fn` keyword, e.g:

- `fn(int): string`
- `fn(string, int=): string` (`=` indicates the parameter is optional)
- `fn(...string): string` (`...` indicates the parameter is variadic)
- `fn(&string): void` (`&` indicates the parameter is by-reference)

I also think restricting this to allow only `Closure` instance inputs
is the right approach ( given that we have FCC ), as i think type
checking array callables, and strings would be annoying and probably
wasteful.

Cheers,
Seifeddine.
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.