Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110449 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 60659 invoked from network); 9 Jun 2020 14:49:50 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 9 Jun 2020 14:49:50 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CAA651804FD for ; Tue, 9 Jun 2020 06:33:32 -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.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-lj1-f194.google.com (mail-lj1-f194.google.com [209.85.208.194]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 9 Jun 2020 06:33:32 -0700 (PDT) Received: by mail-lj1-f194.google.com with SMTP id s1so25080524ljo.0 for ; Tue, 09 Jun 2020 06:33:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=4et6hhNvqCS7nI3fWvmamVkDxKWSZZV2TO4Vw1Yy5yI=; b=tEkSjrIjEmlz9plbpD8hjV6TNaKfdePC2MCEZn/wU2NElVNYzUGB+BtToyppY+soCx f9CFUUaifiN+iJhuGvW4DyQhYXP7PGy+VGMiZz2VELwgImtfXGyd+UgwviWdxhN+o7QS NUkthsl7ROiAqyK7q1wheKmqhUfQSfwcAfyuIB0NMSmoPmCqzo+3D5k5kZVUQNwwMDHT D4GcPnmN/TGRjY42jI0SsZP5k4v6pWuqOhFcCpqFyy3ikWB/fH3PHXi6NjRY4ntVeaWU 5DWDkoEZv+J7hhkqqcKvJgNPSGKmQSppzCRcv0fAlMO9FM6zf+KPj7ntzBlkxPdfKXG0 rKRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=4et6hhNvqCS7nI3fWvmamVkDxKWSZZV2TO4Vw1Yy5yI=; b=hkMTFlyNqsI1/YP9eZ6PJFuEPwIySoeYdWPif2cobvaV27MgjmidPxhRhn6turglO4 FZWGgFNCYgpL7NI/H3Y16dEeCi9qiMx9mSmuwhK6dstH0Z3gYhRFMkdpV7m0/50jrElr jcD8leKFrq8T11PQdRm2jJLGsuJ0id1n27H+1w+Qw99KarNPTQj9F1aXAX8uhnNs4rfv Z9ComuHxR1lKkfGzqCsU4kGnARsBJxkNelkzBcTAEVEb42DTE1ZtKm3LIgtO2iV2JtZP eQR/qeFexslmdLH/BcOld34B3NGJVV2IbaQfaohf+j2FxP+A3G3wEhbiyQaxajbDo5Z9 /dBA== X-Gm-Message-State: AOAM5313mz9xF80gbdDBKGIff0rw+kRnGi0VQ+Il9bMiQdlkv3bvdII2 JjRHvYw7J/SWO4oq2et1jPQrnCm3l9x59bgBFMoonNPB X-Google-Smtp-Source: ABdhPJxngq80i8rQg7121z0hfw3J9eFHCg7SAjl6sX65vULNhHPxVqs2NszCmPQA+h3aLE5FXUmZofoUScPNAhsjsYQ= X-Received: by 2002:a2e:9115:: with SMTP id m21mr13228772ljg.350.1591709609293; Tue, 09 Jun 2020 06:33:29 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 9 Jun 2020 15:33:13 +0200 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="000000000000d8fd9105a7a6c578" Subject: Re: Making all Traversables an Iterator or IteratorAggregate From: nikita.ppv@gmail.com (Nikita Popov) --000000000000d8fd9105a7a6c578 Content-Type: text/plain; charset="UTF-8" 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. Regards, Nikita --000000000000d8fd9105a7a6c578--