Re: "Reader" as alternative to Iterator
| From: | Andreas Hennings | Date: | Mon, 03 Jul 2017 16:08:42 +0000 |
| Subject: | Re: "Reader" as alternative to Iterator | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-99715@lists.php.net to get a copy of this message | ||
On Mon, Jul 3, 2017 at 5:53 PM, Johannes Schlüter
<johannes@schlueters.de> wrote:
> On Mo, 2017-07-03 at 17:32 +0200, Niklas Keller wrote:
>
>> > That distinction is the reason why next() and valid() are different
>> > methods in iterators.
I would rather say this is the reason why current() and valid() are
different methods.
A combination of ->current() and next() would not cause problems.
>>
>> Not really, Iterator::next() returns void, so could as well return
>> bool.
>
> Well, that story is a bit longer and I cut it short. Let's assume we
> remove valid and use next's return value. Then we don't know if the
> first element exists or not, as next is only called after the first
> iteration. An alternative might be using only current() but then we
> need a special, magic, return value to mark the end oder references.
> Both are bad.
The proposed ->read() method is more or less the same as ->current()
plus ->next(), if ->current() always returns FALSE on end-of-data.
An object that has both ->read() and ->valid() would remove the need
for a special magic return value.
But in many many cases, having a magic return value FALSE is totally acceptable.
Think of fgetcsv(), fread(), or PDOStatement::fetch().
So if we wanted, we could have one interface "SimpleReader" or
"FalseTerminatedReader" with only the ->read() method, and another
method "Reader" with both ->read() and ->valid().
(Sometimes I would like to have a new data type "unique symbol" for
constants that are not strings or integers. So we could say "while
(EOF !== $value = $reader->read());" but this is another topic)
>
> johannes