Re: "Reader" as alternative to Iterator
| From: | Rowan Collins | Date: | Wed, 05 Jul 2017 10:51:19 +0000 |
| Subject: | Re: "Reader" as alternative to Iterator | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-99759@lists.php.net to get a copy of this message | ||
On 3 July 2017 16:58:16 BST, Andreas Hennings <andreas@dqxtech.net> wrote:
>My motivation was to be able to use iterators and readers
>interchangeably.
>Readers are easier to implement as classes.
>Iterators have the benefit of the generator syntax.
>The idea is to have a library where some stuff is implemented as
>reader classes, and some other things are implemented as iterator
>classes.
>
>Then for the actual operational code, I need to choose to use either
>iterator syntax or reader syntax. So either I need adapters for all
>iterators, or I need adapters for all readers.
An alternative to adapters in this case might be a Trait that allows you to write the class as a
Reader, but turn it into a fully-functioning Iterator with one line of code. If you always call your
read method read() you only need a single adapter which calls that at the appropriate times and
buffers the value to know if it's reached the end. That adapter could be "pasted" by
a Trait into each class where read() is defined, and read() could even be private.
Regards,
--
Rowan Collins
[IMSoP]