Re: Consolidation of Traversable and array operations

From: Date: Thu, 16 Jul 2015 09:26:31 +0000
Subject: Re: Consolidation of Traversable and array operations
References: 1 2 3 4 5 6  Groups: php.internals 
Request: Send a blank email to internals+get-87194@lists.php.net to get a copy of this message
Benjamin Eberlei wrote on 15/07/2015 21:19:
On Wed, Jul 15, 2015 at 9:53 PM, Rowan Collins <rowan.collins@gmail.com <mailto:rowan.collins@gmail.com>> wrote:
    On 15 July 2015 20:39:05 GMT+01:00, Levi Morrison <levim@php.net
    <mailto:levim@php.net>> wrote:
Do note that the union types RFC would also alleviate some of this pain for user-land functions, since array | Traversable would cover the required access pattern. I think this is a superior approach myself, but I may be a bit biased as I am authoring that proposal: https://wiki.php.net/rfc/union_types
    I like the idea of union types in general, but have always felt
    that a typehint of Traversable should accept arrays as a matter of
    course. The only thing such a typehint guarantees is that foreach
    will work (there are no methods defined that can be called
    directly), and an array meets that condition. The only difference
    I can see is in the passing / mutability behaviour of objects vs
    non-objects.
But instanceof and anything related to Reflection or get_class or relevant code will fail on array.
Ah, yes, I hadn't thought of reflection type things. I don't think instanceof would be a problem, because checking for any class or interface other than Traversable would correctly return false; not sure if it would make sense for "array() instanceof Traversable" to return true or not...

« previous php.internals (#87194) next »