Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:61214 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 12844 invoked from network); 14 Jul 2012 00:27:03 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Jul 2012 00:27:03 -0000 Authentication-Results: pb1.pair.com smtp.mail=larry@garfieldtech.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=larry@garfieldtech.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain garfieldtech.com from 66.111.4.27 cause and error) X-PHP-List-Original-Sender: larry@garfieldtech.com X-Host-Fingerprint: 66.111.4.27 out3-smtp.messagingengine.com Received: from [66.111.4.27] ([66.111.4.27:41749] helo=out3-smtp.messagingengine.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id D2/92-20866-5DCB0005 for ; Fri, 13 Jul 2012 20:27:02 -0400 Received: from compute1.internal (compute1.nyi.mail.srv.osa [10.202.2.41]) by gateway1.nyi.mail.srv.osa (Postfix) with ESMTP id 1CF5A20B4F for ; Fri, 13 Jul 2012 20:26:58 -0400 (EDT) Received: from frontend2.nyi.mail.srv.osa ([10.202.2.161]) by compute1.internal (MEProxy); Fri, 13 Jul 2012 20:26:59 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=message-id:date:from:mime-version:to :subject:references:in-reply-to:content-type :content-transfer-encoding; s=smtpout; bh=+t8pQg35n9MVOAx/kBbnoV EZ76M=; b=BRV9088aAe1f5pmdwQGvbgcTLmmdH4cipWPCIPTYuTTFF88A7/c2ma 8vb7C0VTpcQ+l5iKNA2g+dHeRkDKBBC6zgKxQIauWPhKtnE0DvjEGemYLYJ2vDTu PXdzBgYtRlFE1wG5JUfpa2tz0UpZOkX1QrtrbMeVAjuiCH7rsiLNA= X-Sasl-enc: PRWLzciC6Xut4/CpIzQgqZW3Rfa2D8QR6YQyonZCyMB7 1342225618 Received: from Palantirs-MacBook-Pro.local (unknown [209.41.114.202]) by mail.messagingengine.com (Postfix) with ESMTPA id B882048360A for ; Fri, 13 Jul 2012 20:26:58 -0400 (EDT) Message-ID: <5000BCD2.2040007@garfieldtech.com> Date: Fri, 13 Jul 2012 19:26:58 -0500 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:13.0) Gecko/20120614 Thunderbird/13.0.1 MIME-Version: 1.0 To: internals@lists.php.net References: <1342050547.1456.33.camel@guybrush> <4FFF6C9C.2080802@gmail.com> <36.A6.17670.8E820005@pb1.pair.com> <50005B38.3090507@sugarcrm.com> In-Reply-To: <50005B38.3090507@sugarcrm.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Internal iteration API From: larry@garfieldtech.com (Larry Garfield) On 7/13/12 12:30 PM, Stas Malyshev wrote: > Hi! > >> Yep, today we have : >> - arrays >> - Object implementing ArrayAccess >> - Objects implementing Traversable (Iterator) >> - array_****() functions not working with ArrayAccess' objects >> - Iterator API not working with arrays (ArrayIterator needed) >> - ... I forget things >> >> There sure is something to do with that to make a consistent API. > > Definitely, having consistent generic iteration API would be great, and > pretty much the only reason many iterative functions don't suport > traversables is that there's no such API. > > As for ArrayAccess, it's more complicated. For example, if you have > array_shift() I'm not sure ArrayAccess is enough to implement it. To > find out "first" element you'd need Traversable, but even that won't > allow you to "shift" array elements. So for some array_* functions only > thing that you can rely on is an actual array... So, I've not been inside the engine so in practice this may make no sense at all, but what about looking at it the other way around? Vis, instead of making objects more and more like arrays, make arrays an object that happens to implement ArrayAccess, Iterator, and whatever else, with an implementation that matches its current behavior? I mean, it's not like they're actual language arrays now, they're hash maps with a convenient syntax. Then the array_* functions that need to be able to iterate can internally type hint Traversable (and thus take any Traversable, of which a hashmap would be one), those that need to []-access can type hint ArrayAccess, and those that need both can hint, hum, TraversableArrayAccess or something (that extends both of those). Effectively, turn array() and [...] into syntactic sugar on top of a "HashMap implements ArrayAccess, Traversable" object, much as anonymous functions are, as I understand it, syntactic sugar on top of an object that has an __invoke() method. Combined with the generator proposal and recent improvements to filters, that could lead to some seriously slick potential. $hash_map_of_type_MyIteratableClass = array_map($some_closure, $generator_of_some_sort, 'MyIterableClass'); Would that alleviate the "oh god it's two things" problem without causing too many other issues? --Larry Garfield