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 18:03] – Add explicit example demonstrating return behavior when generator throws rdlowrey | rfc:generator-return-expressions [2015/03/17 14:48] – Update status to accepted rdlowrey | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Generator Return Expressions ====== | ====== PHP RFC: Generator Return Expressions ====== | ||
- | * Version: | + | * Version: |
* Date: 2015-02-18 | * Date: 2015-02-18 | ||
- | * Author: Daniel Lowrey, rdlowrey@php.net | + | * 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 105: | Line 105: | ||
- | Calling '' | + | Calling '' |
<code php> | <code php> | ||
Line 117: | Line 117: | ||
$bar = foo(); | $bar = foo(); | ||
- | try { | + | |
- | $bar->current(); | + | set_exception_handler(function($e1) use ($bar) |
- | } catch (Exception $e) { | + | |
- | | + | |
- | } | + | } catch (Exception $e2) { |
+ | // Generator::getReturn() | ||
+ | | ||
+ | // is a logic error we want to prevent. | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | $bar-> | ||
</ | </ | ||
Line 128: | 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 163: | 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 217: | 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 '' | ||
- | ===== Proposed Voting Choices ===== | + | <code php> |
- | Two voting choices are proposed: | + | <?php |
+ | function foo() { | ||
+ | yield 1; | ||
+ | return yield from bar(); | ||
+ | } | ||
- | 1. **YES**, allow Generator return expressions and '' | + | function bar() { |
+ | yield 2; | ||
+ | yield 3; | ||
+ | return 4; | ||
+ | } | ||
- | 2. **NO**, do not modify existing Generator behavior | + | $baz = foo(); |
+ | foreach ($baz as $element) { | ||
+ | echo $element, " | ||
+ | } | ||
+ | echo $baz-> | ||
+ | |||
+ | // 1 | ||
+ | // 2 | ||
+ | // 3 | ||
+ | // 4 | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Vote ===== | ||
+ | |||
+ | Voting begins 2015-03-09 and ends on 2015-03-16. | ||
A 2/3 " | A 2/3 " | ||
+ | |||
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | |||
+ | . Vote closed at 14:50 UTC 2015-03-17. | ||
+ | |||
+ | **NOTE:** the related [[rfc: | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
- | There is as yet no patch for the proposed functionality. | + | |
+ | https:// | ||
+ | |||
+ | The linked | ||
Line 240: | Line 308: | ||
[[https:// | [[https:// | ||
- | ===== Rejected Features ===== | ||
- | TBD |
rfc/generator-return-expressions.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1