Re: Introducing "Array Of" RFC

From: Date: Mon, 20 Jan 2014 07:31:17 +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-71300@lists.php.net to get a copy of this message
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". Finally when arrays are copied, and "member type" is already set, copy it along with the array, Optionally some of the array functions like array_merge might be made aware of the scheme, too. BTW, I'm undecided regarding the need for the overall feature, from my rather limited personal coding practise it seems to me that whenever I am interested in member type checking I'm already looping over the array and can easily add up-front instanceof checks to the loops with suitable explicit error-out. Which I generally prefer over automatic errors, especially fatal ones, because _I_ control the error case behaviour. But I think _if_ such a scheme were added, the implementation described above would solve the O(N*M) problem with acceptable overall cost. best regards Patrick

« previous php.internals (#71300) next »