Re: Introducing "Array Of" RFC
On 01/19/2014 10:19 PM, Rasmus Lerdorf wrote:
On 1/19/14, 9:13 AM, Philip Sturgeon wrote:
I think its a nice logical improvement which syntactically offers a
replacement for looney boilerplate code, exactly like Variadics which
a LOT of people are excited about.
Actually variadics solves a problem that can't be solved other ways,
namely by-ref for an unknown number of arguments. There is no userspace
boilerplate code, looney or otherwise, that can do that.
I won't argue whether the base need is great enough here. I am sure it
is to some people. I do have an issue with the idea that the right
solution is a full hash table scan on every function call. I have a
feeling that if this makes it in this is one of those features that will
be high my list for optimization/refactoring when I run across it in
code. As in, replace the per-call hash scan with a single userspace scan
that is only done once earlier in the stack. From a performance
perspective the only-done-once userspace version is extremely likely to
outperform this built-in per-call check assuming more than a trivial
amount of calls to the function in question.
-Rasmus
Which I think goes to the earlier point about it being better to do at add-time than read-time, since add-time is O(n) and read-time is O(n*m).
Stepping back a bit, the underlying feature request, essentially, is "I want to ensure I have a collection of X without having to write a custom collection class every frickin' time". (Writing a FooCollection class whose add() method only accepts Foo objects is very straightforward, but generally needless boilerplate.) That can be checked at add time or read-time. I think that's an entirely valid use case in modern code, and can help lead to more self-documenting code, too.
The patch in the RFC handles that with arrays as the collection, and run-time checking via type hints. My initial thought was "yes please!", but from the discussion here I can see where that's sub-optimal for performance reasons as Rasmus notes.
Generics or something in SPL would allow for simpler add-time checking, which is what we probably want. However, if we did that then all of the various array functions don't work. That's hardly a new problem for PHP, of course.
So how could we add "collection of just X type of things" support to PHP without running afoul of the "ArrayObject vs. Array" problem?
I'm not sure, but I think this is another case where "arrays as the uber-data structure" gets in the way of more robust programming methodologies.
--Larry Garfield
Thread (73 messages)