On 15.04.2016 at 17:42, Larry Garfield wrote:

> I think there's 2 general use cases for union types that would be "good
> things", which are different for & and |, and have very little...
> intersection.  (*yaaaaaaa!*)
> 
> The OR case is for cases where the language doesn't support a unified
> case at all.  The most obvious example here is array|Traversable.  If I
> want "a thing I can foreach()", then PHP right now has no way of saying
> that syntactically.  You have to type on array, or Traversable, or not
> type at all.  array|Traversable is what you really want,

It is not what I would want, though.

> because those
> DO have an overlap (foreach-ablility), PHP is just incapable of
> representing that otherwise.

Maybe we should consider to accept an array as Traversable?  Actually, I
wonder why that's not already the case.

> A similar example would be callable|SomeInterface.  An interface can
> specify a signature for __invoke(), which gives you documentation on the
> format that is expected for a callable.  However, you can't strictly
> enforce that because then you don't allow for a function or closure that
> fits the same method signature.  That means you have to leave it
> untyped.  This, I argue, would be better *and* reasonably type safe:
> 
> interface MiddlewareInterface {
>   function __invoke(RequestInterface $req, ResponseInterface $res);
> }
> 
> function middleware_builder(callable|MiddlewareInterface $m) {
>   // ...
> }
> 
> As that self-documents that MiddlewareInterface is the callable
> signature we need, but still allows an arbitrary callable to be passed. 
> It's not perfect (I could pass a string of a function name that doesn't
> have that interface and it would still explode), but it is an
> improvement over middleware_builder() having no type specification at
> all, as is the case today.

In my opinion, `callable' is to weak a type hint to be really useful,
and it would be better if we would improve that (generics come to mind).
 Then you wouldn't need MiddlewareInterface at all and be not afraid
that somebody passes in an incompatible function.

-- 
Christoph M. Becker


-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to