rfc:generator-return-expressions
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
rfc:generator-return-expressions [2015/02/19 16:30] – created rdlowrey | rfc:generator-return-expressions [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
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 61: | Line 61: | ||
// 1 | // 1 | ||
// 2 | // 2 | ||
- | // 42 | ||
// int(42) | // int(42) | ||
</ | </ | ||
+ | |||
+ | |||
+ | Calling '' | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | function foo() { | ||
+ | yield 1; | ||
+ | yield 2; | ||
+ | yield 3; | ||
+ | } | ||
+ | |||
+ | $bar = foo(); | ||
+ | while($bar-> | ||
+ | $bar-> | ||
+ | } | ||
+ | |||
+ | assert($bar-> | ||
+ | </ | ||
+ | |||
Calling '' | Calling '' | ||
Line 85: | Line 104: | ||
</ | </ | ||
- | Calling '' | + | |
+ | Calling '' | ||
<code php> | <code php> | ||
<?php | <?php | ||
function foo() { | function foo() { | ||
+ | throw new Exception; | ||
yield 1; | yield 1; | ||
yield 2; | yield 2; | ||
- | | + | |
} | } | ||
$bar = foo(); | $bar = foo(); | ||
- | while($bar-> | ||
- | $bar-> | ||
- | } | ||
- | assert($bar-> | + | set_exception_handler(function($e1) use ($bar) { |
+ | try { | ||
+ | | ||
+ | } catch (Exception $e2) { | ||
+ | // Generator:: | ||
+ | // value from a generator that didn't actually complete | ||
+ | // is a logic error we want to prevent. | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | $bar-> | ||
</ | </ | ||
Line 106: | 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 141: | 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 190: | Line 243: | ||
===== Unaffected PHP Functionality ===== | ===== Unaffected PHP Functionality ===== | ||
- | Existing generator semantics remain unmodified. Only new functionality is added to allow generators to return expressions via '' | + | Existing generator semantics remain unmodified. Only new functionality is added to allow generators to '' |
===== 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 218: | Line 308: | ||
[[https:// | [[https:// | ||
- | ===== Rejected Features ===== | ||
- | TBD |
rfc/generator-return-expressions.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1