Re: Internal iteration API
On Thu, 2012-07-12 at 00:17 +0200, Nikita Popov wrote:
> Hi internals!
>
> Currently PHP does not have an internal iteration API that supports
> both arrays and Traversable objects.
Yes. in fact doing that is long overdue.
> Because of that it is currently
> not really possible to write functions (or language features) that
> work on arbitrary traversables. That's probably also the reason why
> function like array_sum() currently work only on arrays and arrays
> only.
One thing to keep in mind when doing this is to think about consistency.
Right there's quite a distinction. Things either take an array or a
Traversable object. We should think about not creating a mess when some
functions, especially ones called array_foo() allow Traversable while
others won't. So we might need the same infrastructure in regards to
ArrayAccess to help this a bit.
Just picking some random example:
* array_map() - This can be implemented using that infrastructure.
(While some might think about returning an Traversable object if
a Traversable object got put in, maybe a FilterIterator but
let's ignore that)
* array_filter() - This is almost the same as array_map() but we
can't implement it using Traversable, as current() (for good
reasons) returns no reference.
Another "fun fact" might be that Traversable doesn't require keys to be
unique. This can have strange results for things like array_map().
Just to be clear I fully support it, but this is an area which clearly
requires some more global design. Maybe even going through all functions
processing arrays and categorizing them to see the impact.
Ah, and maybe completely unrelated to the things above but not
to forget: When implementing this the code becomes more complex
as exceptions thrown in key(), current(), rewind() have to be
caught. With "classic" zend_hash iteration those operations will
succeed if there was a next element pointer. Maybe that can be
handled in a generic infrastructure, so we have less errors
later.
johannes
Thread (24 messages)