Bug #78434 [Com]: Generator yields no items after valid() call
| From: | enumag at gmail dot com | Date: | Wed, 21 Aug 2019 13:28:35 +0000 |
| Subject: | Bug #78434 [Com]: Generator yields no items after valid() call | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-222344@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=78434&edit=1
ID: 78434
Comment by: enumag at gmail dot com
Reported by: enumag at gmail dot com
Summary: Generator yields no items after valid() call
Status: Open
Type: Bug
Package: *General Issues
Operating System: irrelevant
PHP Version: 7.4.0beta2
Block user comment: N
Private report: N
New Comment:
As far as I know calling Generator::rewind() is alright until next() is called for the first time.
Which means that your argument whether or not should yield from call rewind() is not relevant here -
the result would be the same regardless.
Previous Comments:
------------------------------------------------------------------------
[2019-08-21 13:24:38] enumag at gmail dot com
> To make this more obvious, replace valid() with current() -- should the yield from repeat the
> value you already fetched?
Of course it should. I didn't call next() so the first value was not yet "consumed".
Also as you can see in the example the behaviour is inconsistent between the first and second yield
from. That's definitely not correct.
------------------------------------------------------------------------
[2019-08-21 13:03:49] nikic@php.net
The correct behavior here depends on whether "yield from" is supposed to iterate from the
current position of the generator, or from the start (and throw if we're not at the start). To
make this more obvious, replace valid() with current() -- should the yield from repeat the value you
already fetched?
Generally I would agree that it should iterate from the start, in particular because this is what
yield from does for non-generator Iterators.
------------------------------------------------------------------------
[2019-08-21 10:02:01] enumag at gmail dot com
Description:
------------
See the example script. The inner $function generator is called twice so two zeros should be yielded
from $wrapper. Yet only one is yielded if I call valid() on the Generator before using yield from.
Test script:
---------------
https://3v4l.org/GfJHG
Expected result:
----------------
00
Actual result:
--------------
0
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=78434&edit=1