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

[email protected] (Matheus Martins)
Newsgroups php.internals
Message-ID <CAAZKEqiNx0voS4bcobUMz1vgACEAsmi6gDkKRXwitjV=cePFgQ@mail.gmail.com>
> I would find this very useful

Glad we agree it brings value.

> That said, restricting it to closures and not any callable is very limiting,
> particularly now that we have first-class callable syntax.
> Perhaps something more along the line of: callable(int):bool

I actually read first-class callables the other way -- as what makes
Closure-only *not* limiting: `strlen(...)`, `$obj->method(...)` and
`Foo::bar(...)`
all produce a Closure, so any callable can be passed by appending (...).

That is relevant, because this is a runtime check, not a static one. A Closure
is already resolved -- scope-bound, with its signature on the object, no lookup
and no side effects. A callable is not: 'foo' resolves against the caller's
namespace, and [Foo::class, 'bar'] must be resolved and possibly autoloaded
before its signature is even visible. Enforcing callable(int): bool at a
boundary would mean doing that resolution -- autoload included -- mid-call,
which is exactly the surprise I want to keep out of a type check. (callable also
cannot be a property type today, while Closure can.)
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.