Bug->Doc #71252 [Opn]: Yield from by reference is not supported

From: Date: Wed, 22 Sep 2021 12:07:35 +0000
Subject: Bug->Doc #71252 [Opn]: Yield from by reference is not supported
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-19207@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71252&edit=1 ID: 71252 Updated by: cmb@php.net Reported by: nikic@php.net -Summary: yield from ignores yield-by-reference +Summary: Yield from by reference is not supported Status: Open -Type: Bug +Type: Documentation Problem Package: Scripting Engine problem PHP Version: 7.0Git-2015-12-30 (Git) Block user comment: N Private report: N New Comment: As of PHP 7.0.4 this is forbidden[1]. When looking at the respective commit message[2] and the now long standing behavior, it might be best to just document the behavior. [1] <https://3v4l.org/fZEH0> [2] <https://github.com/php/php-src/commit/ccc06e252bdf4c54cb2c96dbcab9a2aeb933181c> Previous Comments: ------------------------------------------------------------------------ [2015-12-30 23:24:07] nikic@php.net Description: ------------ When "yield from" was implemented, we did not consider how it interacts with yield-by-reference. Right now it is simply ignored and "yield from" yields all values by-value. Likely this is not the expected or intended behavior. Instead "yield from" in a by-reference generator should yield array values and values from by-reference generators by-reference. "yield from" should generate an error for iterators (and generators) that do not support by-reference iteration. Test script: --------------- <?php function &gen(array $array) { yield from $array; } $array = [1, 2, 3]; foreach (gen($array) as &$v) { $v += 1; } var_dump($array); Expected result: ---------------- array(3) { [0]=> int(2) [1]=> int(3) [2]=> int(4) } Actual result: -------------- array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=71252&edit=1

« previous php.doc.bugs (#19207) next »