Bug #75819 [Opn]: Generators have different behavior than Iterators

From: Date: Sun, 21 Jan 2018 14:43:43 +0000
Subject: Bug #75819 [Opn]: Generators have different behavior than Iterators
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-213641@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75819&edit=1 ID: 75819 User updated by: php at fleshgrinder dot com Reported by: php at fleshgrinder dot com Summary: Generators have different behavior than Iterators Status: Open Type: Bug Package: Class/Object related Operating System: - PHP Version: 7.2.1 Block user comment: N Private report: N New Comment: Note that this behavior can be observed only if the Generator is empty, like in the examples. Previous Comments: ------------------------------------------------------------------------ [2018-01-15 17:43:59] php at fleshgrinder dot com Description: ------------ Generators behave differently than Iterators when calling valid on them followed by a loop. A non-rewindable iterator can be asked if it is valid by calling valid on it followed by a loop. The same is not possible with a Generator because it throws an exception stating that an already closed generator cannot be iterated. This is extremely hard to debug because the problem is not apparent at all to the reader of the code. Iterator code: https://3v4l.org/v0qAZ Generator code: https://3v4l.org/2E7EJ Also, note that HHVM/Hack behaves as expected. Test script: --------------- $it = (function () { if (false) { yield 'foo'; }})(); var_dump($it->valid()); foreach ($it as $k => $v) { var_dump($k, $v); } Expected result: ---------------- bool(false) Actual result: -------------- bool(false) Fatal error: Uncaught Exception: Cannot traverse an already closed generator in /in/2E7EJ:7 Stack trace: #0 {main} thrown in /in/2E7EJ on line 7 Process exited with code 255. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=75819&edit=1

« previous php.bugs (#213641) next »