Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:61146 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 90040 invoked from network); 11 Jul 2012 23:49:15 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Jul 2012 23:49:15 -0000 Authentication-Results: pb1.pair.com header.from=johannes@schlueters.de; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=johannes@schlueters.de; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain schlueters.de from 217.114.211.66 cause and error) X-PHP-List-Original-Sender: johannes@schlueters.de X-Host-Fingerprint: 217.114.211.66 config.schlueters.de Received: from [217.114.211.66] ([217.114.211.66:43270] helo=config.schlueters.de) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 0C/52-09688-BF01EFF4 for ; Wed, 11 Jul 2012 19:49:15 -0400 Received: from [192.168.2.230] (ppp-188-174-38-90.dynamic.mnet-online.de [188.174.38.90]) (using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits)) (Client did not present a certificate) by config.schlueters.de (Postfix) with ESMTPSA id 4767A626D5; Thu, 12 Jul 2012 01:49:12 +0200 (CEST) To: Nikita Popov Cc: PHP internals In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Date: Thu, 12 Jul 2012 01:49:07 +0200 Message-ID: <1342050547.1456.33.camel@guybrush> Mime-Version: 1.0 X-Mailer: Evolution 2.30.3 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Internal iteration API From: johannes@schlueters.de (Johannes =?ISO-8859-1?Q?Schl=FCter?=) 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