rfc:generator-return-expressions
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:generator-return-expressions [2015/02/19 23:34] – s/background/concurrent/ rdlowrey | rfc:generator-return-expressions [2015/03/09 16:33] – Move to voting status rdlowrey | ||
---|---|---|---|
Line 2: | Line 2: | ||
* Version: 0.1 | * Version: 0.1 | ||
* Date: 2015-02-18 | * Date: 2015-02-18 | ||
- | * Authors: Daniel Lowrey < | + | * Author: Daniel Lowrey < |
- | * Status: | + | * Contributors: |
+ | * Status: | ||
* First Published at: http:// | * First Published at: http:// | ||
Line 9: | Line 10: | ||
- | PHP's generators are unequivocally useful both for iteration and cooperative multi-tasking. However, the inability of generator functions to specify return values artificially limits their usefulness for multitasking in coroutine contexts. This RFC proposes the ability to both specify and access Generator return values while laying the groundwork for future sub-generator returns. | + | PHP's generators are unequivocally useful both for iteration and cooperative multi-tasking. However, the inability of generator functions to specify return values artificially limits their usefulness for multitasking in coroutine contexts. This RFC proposes the ability to both specify and access Generator return values while laying the groundwork for future sub-generator returns. The proposal is a prerequisite for the conceptually related [[rfc: |
Line 31: | Line 32: | ||
- | 1. Modify generator functions to allow '' | + | * Modify generator functions to allow '' |
- | 2. Expose '' | + | * Expose '' |
- | 3. Calling '' | + | * Calling '' |
Line 52: | Line 53: | ||
$bar = foo(); | $bar = foo(); | ||
- | do { | + | foreach ($bar as $element) |
- | echo $bar-> | + | echo $element, " |
- | $bar-> | + | } |
- | } while ($bar-> | + | |
var_dump($bar-> | var_dump($bar-> | ||
Line 135: | Line 135: | ||
===== Use-Case: Coroutine Return Values ===== | ===== Use-Case: Coroutine Return Values ===== | ||
- | Generators are particularly useful for their ability to pause execution and resume at a later time. This capacity allows applications to cooperatively multitask discrete units of processing work. However, the inability to explicitly return values leaves coroutines in a situation where they' | + | Generators are particularly useful for their ability to suspend |
<code php> | <code php> | ||
Line 170: | Line 170: | ||
return $bar + 42; // unambiguous execution " | return $bar + 42; // unambiguous execution " | ||
}; | }; | ||
+ | ?> | ||
</ | </ | ||
+ | |||
+ | |||
+ | ===== Reference Returns ===== | ||
+ | |||
+ | Generators currently utilize the `&` operator to indicate values will be yielded by-reference: | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | function & | ||
+ | $value = 3; | ||
+ | |||
+ | while ($value > 0) { | ||
+ | yield $value; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | foreach (gen_reference() as & | ||
+ | echo (--$number).' | ||
+ | } | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | As '' | ||
Line 224: | Line 248: | ||
===== Future Scope ===== | ===== Future Scope ===== | ||
- | The proposed behavior lays the groundwork for future sub-generator functionality using '' | + | The proposed behavior lays the groundwork for future sub-generator functionality using '' |
A brief example of how '' | A brief example of how '' | ||
Line 232: | Line 256: | ||
function foo() { | function foo() { | ||
yield 1; | yield 1; | ||
- | | + | |
- | yield $subReturn; | + | |
} | } | ||
Line 243: | Line 266: | ||
$baz = foo(); | $baz = foo(); | ||
- | + | foreach ($baz as $element) | |
- | do { | + | echo $element, " |
- | echo $baz-> | + | } |
- | | + | echo $baz->getReturn(), " |
- | } while($baz->valid()); | + | |
// 1 | // 1 | ||
Line 256: | Line 278: | ||
- | ===== Proposed Voting Choices | + | ===== Vote ===== |
- | Two voting choices are proposed: | + | |
- | 1. **YES**, allow Generator return expressions | + | Voting begins 2015-03-09 |
- | + | ||
- | 2. **NO**, do not modify existing Generator behavior | + | |
A 2/3 " | A 2/3 " | ||
+ | |||
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | |||
+ | . | ||
+ | |||
+ | **NOTE:** the related [[rfc: | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
Line 279: | Line 308: | ||
[[https:// | [[https:// | ||
- | ===== Rejected Features ===== | ||
- | TBD |
rfc/generator-return-expressions.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1