Re: Introducing "Array Of" RFC

From: Date: Mon, 20 Jan 2014 08:46:49 +0000
Subject: Re: Introducing "Array Of" RFC
References: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19  Groups: php.internals 
Request: Send a blank email to internals+get-71310@lists.php.net to get a copy of this message
hi, On Mon, Jan 20, 2014 at 9:05 AM, Rasmus Lerdorf <rasmus@lerdorf.com> wrote: > On 1/19/14, 11:31 PM, Patrick Schaaf wrote: >> Regarding the N*M checking complexity, couldn't a little bit of memory >> alleviate the problem? >> >> For each array, have a "member type" pointer, initially NULL. >> >> When the member type needs to be checked for the first time, and that >> "member type" pointer is not already set, do an O(N) scan of the arrray, >> testing compatibility of all members against the hinted type. Error on >> mismatch, but when all members match, remember the type behind the "member >> type" pointer. >> >> When the member type needs to be checked and the "member type" pointer _is_ >> set, just compare it to the hinted type O(1). >> >> There must be some overhead when elements are added to an array. Either >> "when member type is set, forget it", or even "when member type is already >> set, check compatiility of the newly added element with the member type, >> and then remove member type on mismatch or keep it on match". > > This means every HT becomes larger now and every time you write to a HT > you at the very least need to do one extra comparison. We use HTs > everywhere, so this would have a performance impact on every script even > if you have absolutely no intention of ever using this feature. As I like this RFC for other reasons (type hinting extension), I tend to think that the use cases presented here can be done in a more efficient way. Someone else mentioned "collections" and it is exactly what it is all about. A collection could be an instance of a given class, which stores only one class type. It prevents on usage/function call checks and checks are done only when adding a member to the collection. On another note, and only as a note here as I really do not want to hijack this thread with another topic, the more I see this kind of RFCs the more I feel like we are heading to general or global type hinting supports. I think it could be the right time to actually decide what we want. It is getting critical as we keep adding stuff about type hinting, one after another, which may lead to inconsistencies and will prevent us to have a good type hinting mechanism in PHP. Cheers, -- Pierre @pierrejoye | http://www.libgd.org

« previous php.internals (#71310) next »