From: Operating system: Irrelevant PHP version: 5.3.2 Package: *Programming Data Structures Bug Type: Feature/Change Request Bug description:yield syntax construct
Description: ------------ I draw your attention to the "yield" construct as used in Python, (recent) JavaScript, and C#. This would be I think a nice way of creating Iterators for traversing complex objects. A method that uses "yield" can be replaced by one that declares an instance of a custom implementation of Iterator that takes $this and the method's arguments in its constructor, and cranks through a state machine on each call of next(). (Microsoft's C# compiler handles it in essentially this way, but has a much harder time of it because of things like multithreading and explicit resource disposal.) To some extent this can be done by hand now (I've done some work on a preprocessing script that automates this) but in the absence of inner classes (which is what this Iterator ought to be) - hint, hint - it plays havoc with visibility attributes. Expected result: ---------------- /* Example method that produces an inorder traversal of a binary tree. foreach($btree->traverse_inorder() as $node) { do_something_with($node); } */ public function traverse_inorder() { foreach($this->left->traverse_inorder() as $child) { yield $child; } yield $this; foreach($this->right->traverse_inorder() as $child) { yield $child; } } Actual result: -------------- /* The state machine that implements the body of the test script's function (identifies if the end of the traversal has been reached, and obtains the next value to return if not) */ do { switch($this->_state) { case -1: return false; case 0: // the object being traversed needs "left" made public $this->_t1 = $this->_this->left->traverse_inorder(); reset($this->_t1); $this->_state = 1; case 1: if(!($e = each($this->_t1))) { $this->_t1 = null; $this->_state = 2; continue; } $this->_value = $e['value']; return true; case 2: $this->_value = $this->_this; $this->_state = 3; return true; case 3: // the object being traversed needs "right" made public $this->_t2 = $this->_this->right->traverse_inorder(); reset($this->_t2); $this->_state = 4; case 4: if(!($e = each($this->_t2))) { $this->_t2 = null; $this->_state = -1; return false; } $this->_value = $e['value']; return true; } } while(false); -- Edit bug report at http://bugs.php.net/bug.php?id=51460&edit=1 -- Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=51460&r=trysnapshot52 Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=51460&r=trysnapshot53 Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=51460&r=trysnapshot60 Fixed in SVN: http://bugs.php.net/fix.php?id=51460&r=fixed Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=51460&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=51460&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=51460&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=51460&r=needscript Try newer version: http://bugs.php.net/fix.php?id=51460&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=51460&r=support Expected behavior: http://bugs.php.net/fix.php?id=51460&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=51460&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=51460&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=51460&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=51460&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=51460&r=dst IIS Stability: http://bugs.php.net/fix.php?id=51460&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=51460&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=51460&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=51460&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=51460&r=mysqlcfg