Bug #67821 [Com]: Generator can be rewound
| From: | nikic@php.net | Date: | Mon, 11 Aug 2014 18:37:58 +0000 |
| Subject: | Bug #67821 [Com]: Generator can be rewound | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-187079@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=67821&edit=1
ID: 67821
Comment by: nikic@php.net
Reported by: thekid@php.net
Summary: Generator can be rewound
Status: Not a bug
Type: Bug
Package: Scripting Engine problem
Operating System: Any
PHP Version: 5.5.15
Block user comment: N
Private report: N
New Comment:
rewind() is possible at first yield to support the standard Iterator pattern:
for ($it->rewind(); $it->valid(); $it->next()) {
// ...
}
In which case the rewind is a no-op, priming notwithstanding.
Previous Comments:
------------------------------------------------------------------------
[2014-08-11 10:51:47] thekid@php.net
Hrm, I just see that
Zend/tests/generators/generator_rewind.phpt states:
"A generator can only be rewinded before or at the first yield"
(see also https://wiki.php.net/rfc/generators#rewinding_a_generator)
Again, this does seem inconsistent with the statement "generators will not support
rewinding".
------------------------------------------------------------------------
[2014-08-11 10:34:15] thekid@php.net
Description:
------------
A generator can be rewound if and only if its first yielded value has been fetched. As soon as we
fetch the second (or any further) yielded value and *then* try to rewind it we actually get the
exception. This is inconsistent.
Test script:
---------------
$f= function() { yield 1; yield 2; yield 3; };
$gen= $f();
foreach ($gen as $val) { var_dump($val); }
foreach ($gen as $val) { var_dump($val); } // Exception("Cannot rewind a generator that was
already run")
# Here's the bug:
foreach ($gen as $val) { var_dump($val); break; }
foreach ($gen as $val) { var_dump($val); } // No exception
Expected result:
----------------
int(1)
Exception("Cannot rewind a generator that was already run")
Actual result:
--------------
int(1)
int(1)
int(2)
int(3)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=67821&edit=1