Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110696 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 88711 invoked from network); 22 Jun 2020 17:10:01 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 22 Jun 2020 17:10:01 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C9832180560 for ; Mon, 22 Jun 2020 08:56:59 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS3301 81.224.0.0/12 X-Spam-Virus: No X-Envelope-From: Received: from v-smtpout3.han.skanova.net (v-smtpout3.han.skanova.net [81.236.60.156]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 22 Jun 2020 08:56:58 -0700 (PDT) Received: from [192.168.8.9] ([84.55.126.158]) by cmsmtp with ESMTPA id nOobjFunUnRnenOobjpzTQ; Mon, 22 Jun 2020 17:56:57 +0200 To: Nikita Popov References: Cc: PHP internals Message-ID: <25e60f0d-8057-73ed-9549-65ffaf06d376@telia.com> Date: Mon, 22 Jun 2020 17:56:57 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.9.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB X-CMAE-Envelope: MS4wfCZBp9gBXfyBX7CfPUn+zUeA7V0Id4i02LTNgWDlliDi+wfWRivaQ7dJLyGgYj4Wo21ggJaF3PbsS8w5JQhfVvRkAeHtFk2BGfvc7JLgjvs541Zqoox8 a/rKacBasU5YFbKIwVGhgHizBdSzHvI4TNK/UjpKHxLACUqGJtoMOfcvGXN/n+LgEIKlQyxzxUCQB3l65+j0NcsMee8munGeGGs= Subject: Re: Making all Traversables an Iterator or IteratorAggregate From: bjorn.x.larsson@telia.com (=?UTF-8?Q?Bj=c3=b6rn_Larsson?=) Hi Nikita, Den 2020-06-19 kl. 12:32, skrev Nikita Popov: > On Tue, Jun 9, 2020 at 3:33 PM Nikita Popov wrote: > >> On Tue, May 12, 2020 at 10:26 AM Nikita Popov >> wrote: >> >>> On Wed, Mar 11, 2020 at 10:50 AM Nikita Popov >>> wrote: >>> >>>> Hi internals, >>>> >>>> Userland classes that implement Traversable must do so either through >>>> Iterator or IteratorAggregate. The same requirement does not exist for >>>> internal classes: They can implement the internal get_iterator mechanism, >>>> without exposing either the Iterator or IteratorAggregate APIs. This makes >>>> them usable in get_iterator(), but incompatible with any Iterator based >>>> APIs. >>>> >>> This should have said: "This makes them usable in foreach(), but >>> incompatible with any Iterator based APIs." >>> >>> A lot of internal classes do this, because exposing the userland APIs is >>>> simply a lot of work. I would like to add a general mechanism to make this >>>> simpler: https://github.com/php/php-src/pull/5216 adds a generic >>>> "InternalIterator" class, that essentially converts the internal >>>> get_iterator interface into a proper Iterator. Internal classes then only >>>> need to a) implement the IteratorAggregate interface and b) add a >>>> getIterator() method with an implementation looking like this: >>>> >>>> // WeakMap::getIterator(): Iterator >>>> ZEND_METHOD(WeakMap, getIterator) >>>> { >>>> if (zend_parse_parameters_none() == FAILURE) { >>>> return; >>>> } >>>> zend_create_internal_iterator_zval(return_value, ZEND_THIS); >>>> } >>>> >>>> This allows internal classes to trivially implement IteratorAggregate, >>>> and as such allows us to enforce that all Traversables implement Iterator >>>> or IteratorAggregate. >>>> >>> Does anyone have thoughts on this change? Mostly this is a feature for >>> extensions, but also user-visible in that a bunch of classes will switch >>> from being Traversable to being IteratorAggregate. >>> >>> We may also want to convert some existing Iterators to >>> IteratorAggregates. For example SplFixedArray currently implements >>> Iterator, which means that it's not possible to have nested loops over >>> SplFixedArray. We could now easily fix this by switching it to use >>> IteratorAggregate, which will allow multiple parallel iterators to work on >>> the same array. Of course, there is BC break potential in such a change. >>> >>> There's some bikeshed potential here regarding the class name. I picked >>> "InternalIterator" as an iterator for internal classes, but "internal >>> iteration" is also a technical term (the opposite of "external iteration"), >>> so maybe that name isn't ideal. >>> >> Unfortunately this bikeshed remains unpainted... The proposed names were: >> >> 1. InternalIterator >> 2. ZendIterator >> 3. IteratorForExtensionClassImplementations >> 4. EngineIterator >> >> I'm somewhat partial to the third option, with a less verbose name: >> IteratorForExtensions. >> > I went ahead and changed the implementation to use IteratorForExtensions. > Is anyone overly unhappy with that one? > > @Michal: "ExtensionsIterator" to me sounds like an iterator that iterates > over extensions. > > Regards, > Nikita I'm actually in favour of the term InternalIterator like you first proposed. Internal and external iteration is clearly something different, so no need due to these to shy away here ;-) And today this iterator will be for extensions, but if somehow that would change in the future (which I don't think), option 3 is not ideal. r//Björn L