On 7/13/12 12:30 PM, Stas Malyshev wrote:
Hi!

Yep, today we have :
- arrays
- Object implementing ArrayAccess
- Objects implementing Traversable (Iterator)
- array_****() functions not working with ArrayAccess' objects
- Iterator API not working with arrays (ArrayIterator needed)
- ... I forget things

There sure is something to do with that to make a consistent API.

Definitely, having consistent generic iteration API would be great, and
pretty much the only reason many iterative functions don't suport
traversables is that there's no such API.

As for ArrayAccess, it's more complicated. For example, if you have
array_shift() I'm not sure ArrayAccess is enough to implement it. To
find out "first" element you'd need Traversable, but even that won't
allow you to "shift" array elements. So for some array_* functions only
thing that you can rely on is an actual array...

So, I've not been inside the engine so in practice this may make no sense at all, but what about looking at it the other way around? Vis, instead of making objects more and more like arrays, make arrays an object that happens to implement ArrayAccess, Iterator, and whatever else, with an implementation that matches its current behavior? I mean, it's not like they're actual language arrays now, they're hash maps with a convenient syntax.

Then the array_* functions that need to be able to iterate can internally type hint Traversable (and thus take any Traversable, of which a hashmap would be one), those that need to []-access can type hint ArrayAccess, and those that need both can hint, hum, TraversableArrayAccess or something (that extends both of those).

Effectively, turn array() and [...] into syntactic sugar on top of a "HashMap implements ArrayAccess, Traversable" object, much as anonymous functions are, as I understand it, syntactic sugar on top of an object that has an __invoke() method.

Combined with the generator proposal and recent improvements to filters, that could lead to some seriously slick potential.

$hash_map_of_type_MyIteratableClass = array_map($some_closure, $generator_of_some_sort, 'MyIterableClass');

Would that alleviate the "oh god it's two things" problem without causing too many other issues?

--Larry Garfield

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

Reply via email to