Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:67424 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 31578 invoked from network); 14 May 2013 08:35:54 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 May 2013 08:35:54 -0000 Authentication-Results: pb1.pair.com header.from=ivan.enderlin@hoa-project.net; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=ivan.enderlin@hoa-project.net; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain hoa-project.net from 95.130.12.24 cause and error) X-PHP-List-Original-Sender: ivan.enderlin@hoa-project.net X-Host-Fingerprint: 95.130.12.24 host1.trois-doubles.net Linux 2.6 Received: from [95.130.12.24] ([95.130.12.24:60056] helo=host1.trois-doubles.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 92/A0-27573-567F1915 for ; Tue, 14 May 2013 04:35:52 -0400 Received: from Hwhost2.local (unknown [194.57.88.1]) by host1.trois-doubles.net (Postfix) with ESMTPSA id A19BF20B87D for ; Tue, 14 May 2013 10:35:46 +0200 (CEST) Message-ID: <5191F763.9060008@hoa-project.net> Date: Tue, 14 May 2013 10:35:48 +0200 Reply-To: ivan.enderlin@hoa-project.net User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:22.0) Gecko/20100101 Thunderbird/22.0a2 MIME-Version: 1.0 To: PHP internals Content-Type: multipart/alternative; boundary="------------040607090806080201080608" Subject: MultipleIterator, add a default value From: ivan.enderlin@hoa-project.net ("Ivan Enderlin @ Hoa") --------------040607090806080201080608 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Hi internals, I propose to ameliorate the MultipleIterator class. When a MultipleIterator instance has the MIT_NEED_ANY flag, and when one of the attached iterators has reached its end, the returned value is NULL. I propose to change this behavior by allowing a different default value. Example with no default value: $it = new \MultipleIterator( \MultipleIterator::MIT_NEED_ANY | \MultipleIterator::MIT_KEYS_ASSOC ); $it->attachIterator(new \ArrayIterator(['a', 'b', 'c']), 'foo'); $it->attachIterator(new \ArrayIterator(['x']), 'bar'); foreach($it as $value) echo $value['foo'], ' => ', $value['bar'], "\n"; /** * Will output: * a => x * b => * c => */ Example with 42 as a default value (given as a third parameter of the MultipleIterator::attachIterator method): $it->attachIterator(new \ArrayIterator(['a', 'b', 'c']), 'foo', 42); $it->attachIterator(new \ArrayIterator(['x']), 'bar', 42); foreach($it as $value) echo $value['foo'], ' => ', $value['bar'], "\n"; /** * Will output: * a => x * b => 42 * c => 42 */ It is clearly useful when one of the iterators returns an array, used directly in a loop. To avoid a "NULL pointer exception", we can specify an empty array as a default value. A PHP implementation is pretty easy to do. Naively, we are able to write https://github.com/hoaproject/Iterator/commit/59dcff7f331a7338644cf9a8965d2a934a38661e. If you think it can be useful, I propose to write a RFC with a patch. Thoughts? -- Ivan Enderlin Developer of Hoa http://hoa-project.net/ PhD. student at DISC/Femto-ST (Vesontio) and INRIA (Cassis) http://disc.univ-fcomte.fr/ and http://www.inria.fr/ Member of HTML and WebApps Working Group of W3C http://w3.org/ --------------040607090806080201080608--