# Make Closure covariant to callable

- Thread: https://externals.io/message/125943
- Original archive: https://news-web.php.net/php.internals/125943
- Started by Gina P. Banyard on 2024-11-12T13:53:36+00:00
- Messages: 9
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #125943 — Gina P. Banyard — 2024-11-12T13:53:36+00:00

URL: https://externals.io/message/125943#125943

Hello internals,

Ondřej Mirtes (author of PHPStan) remarked to me in August that Closure is not a subtype of callable.
The implementation of this is quite straight forward and can be seen on GitHub. [1]
The question is if there are any concern or not and if this requires an RFC or can just be merged as is.

Best regards,

Gina P. Banyard

[1] https://github.com/php/php-src/pull/15492

---

## #125944 — Rob Landers — 2024-11-12T14:17:17+00:00

URL: https://externals.io/message/125943#125944 · In reply to: #125943

> Hello internals,
> 
> Ondřej Mirtes (author of PHPStan) remarked to me in August that Closure is not a subtype of callable.
> The implementation of this is quite straight forward and can be seen on GitHub. [1]
> The question is if there are any concern or not and if this requires an RFC or can just be merged as is.
> 
> Best regards,
> 
> Gina P. Banyard
> 
> [1] https://github.com/php/php-src/pull/15492
> 
Hey Gina,

I think saying “Closure should be a subtype of callable” would require an RFC. To say that “Closure should satisfy the callable type check” is just an oversight/bug. They are functionally the same IMO, just different perspectives. 

— Rob

---

## #125945 — Claude Pache — 2024-11-12T16:43:04+00:00

URL: https://externals.io/message/125943#125945 · In reply to: #125943

> Le 12 nov. 2024 à 14:53, Gina P. Banyard <internals@gpb.moe> a écrit :
> 
> Hello internals,
> 
> Ondřej Mirtes (author of PHPStan) remarked to me in August that Closure is not a subtype of callable.
> The implementation of this is quite straight forward and can be seen on GitHub. [1]
> The question is if there are any concern or not and if this requires an RFC or can just be merged as is.
> 
> Best regards,
> 
> Gina P. Banyard
> 
> [1] https://github.com/php/php-src/pull/15492


Hi Gina,

That seems reasonable.

But personally, I would be more interested in having a type equivalent to `callable&object`, that is all objects that implement the `__invoke()` magic method (this is a strict super-type of \Closure).

—Claude

---

## #125946 — Niels Dossche — 2024-11-12T19:13:40+00:00

URL: https://externals.io/message/125943#125946 · In reply to: #125943

> Hello internals,
> 
> Ondřej Mirtes (author of PHPStan) remarked to me in August that Closure is not a subtype of callable.
> The implementation of this is quite straight forward and can be seen on GitHub. [1]
> The question is if there are any concern or not and if this requires an RFC or can just be merged as is.
> 
> Best regards,
> 
> Gina P. Banyard
> 
> [1] https://github.com/php/php-src/pull/15492

Hi Gina

Makes sense to me.

Kind regards
Niels

---

## #125947 — Larry Garfield — 2024-11-12T22:32:26+00:00

URL: https://externals.io/message/125943#125947 · In reply to: #125945

> > […]
>
>
> Hi Gina,
>
> That seems reasonable.
>
> But personally, I would be more interested in having a type equivalent 
> to `callable&object`, that is all objects that implement the 
> `__invoke()` magic method (this is a strict super-type of \Closure).
>
> —Claude

I am in favor of Closure being covarant to callable, and to a new pseudo-type for Closure|Object-with-__invoke.  (Maybe Invokable?)  I do think they'd require brief RFCs, but both would have my support.

--Larry Garfield

---

## #125948 — Gina P. Banyard — 2024-11-12T23:22:31+00:00

URL: https://externals.io/message/125943#125948 · In reply to: #125945

> But personally, I would be more interested in having a type equivalent to `callable&object`, that is all objects that implement the `__invoke()` magic method (this is a strict super-type of \Closure).
> 
> —Claude

An Invokable interface, similar to Stringable would be the solution to this.
And this is something that Ondřej also brought up in the PR.
I would be in favour for such an interface (considering the cat is already out of the bag of auto-implementing things with Stringable).
But I don't think I have the time/bandwidth to work on this, so if anyone wants something "easy" to work on that would be it.

Best regards,

Gina P. Banyard

---

## #125949 — Levi Morrison — 2024-11-13T00:46:16+00:00

URL: https://externals.io/message/125943#125949 · In reply to: #125948

There are some weird cases with `callable` which is why they aren't
allowed as a property type. I don't remember all the edges, but if we
are sure that we cannot hit those with simple co-/contra-variance,
then I don't think this needs an RFC and we can just merge the PR.

---

## #125951 — Gina P. Banyard — 2024-11-13T14:57:02+00:00

URL: https://externals.io/message/125943#125951 · In reply to: #125949

> There are some weird cases with `callable` which is why they aren't
> allowed as a property type. I don't remember all the edges, but if we
> are sure that we cannot hit those with simple co-/contra-variance,
> then I don't think this needs an RFC and we can just merge the PR.

The reason that callable is not allowed as a property type is due to scope/method visibility issues.
See the discussion section of Nikita's Deprecate partially supported callables RFC: https://wiki.php.net/rfc/deprecate_partially_supported_callables#discussion
The callable type is allowed as a parameter and return type, and Closure is "better" defined than just callable.

Best regards,

Gina P. Banyard

---

## #125964 — Levi Morrison — 2024-11-15T17:35:54+00:00

URL: https://externals.io/message/125943#125964 · In reply to: #125951

> > […]
>
> The reason that callable is not allowed as a property type is due to scope/method visibility issues.
> See the discussion section of Nikita's Deprecate partially supported callables RFC: https://wiki.php.net/rfc/deprecate_partially_supported_callables#discussion
> The callable type is allowed as a parameter and return type, and Closure is "better" defined than just callable.

Then personally I see no reason to make this an RFC and would
recommend merging it in as a gap in existing features (not really a
new feature).
