rfc:generator-delegation
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-delegation [2015/03/08 07:40] – v0.2.0 rdlowrey | rfc:generator-delegation [2015/03/16 16:40] – Minor rewording rdlowrey | ||
---|---|---|---|
Line 2: | Line 2: | ||
* Version: 0.2.0 | * Version: 0.2.0 | ||
* Date: 2015-03-01 | * Date: 2015-03-01 | ||
- | * Authors: Daniel Lowrey < | + | * Author: Daniel Lowrey < |
* Contributors: | * Contributors: | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
====== Abstract ====== | ====== Abstract ====== | ||
- | This RFC proposes new '' | + | This RFC proposes new '' |
====== Proposal ====== | ====== Proposal ====== | ||
Line 75: | Line 75: | ||
$send = false; | $send = false; | ||
} else { | } else { | ||
- | | + | throw $e; |
} | } | ||
} | } | ||
Line 101: | Line 101: | ||
its eventual result. | its eventual result. | ||
- | '' | + | Generator delegation |
to allow the decomposition of complex operations into smaller cohesive units. | to allow the decomposition of complex operations into smaller cohesive units. | ||
Line 114: | Line 114: | ||
function myGeneratorFunction($foo) { | function myGeneratorFunction($foo) { | ||
// ... do some stuff with $foo ... | // ... do some stuff with $foo ... | ||
- | $bar = yield * factoredComputation1($foo); | + | $bar = yield from factoredComputation1($foo); |
// ... do some stuff with $bar ... | // ... do some stuff with $bar ... | ||
- | $baz = yield * factoredComputation2($bar); | + | $baz = yield from factoredComputation2($bar); |
return $baz; | return $baz; | ||
Line 135: | Line 135: | ||
==== Use-Case: Generators as Lightweight Threads === | ==== Use-Case: Generators as Lightweight Threads === | ||
- | Perhaps the most powerful | + | The defining |
- | with the capacity | + | for later resumption. This capability gives applications a mechanism to |
implement asynchronous and concurrent architectures even in a traditionally single-threaded language | implement asynchronous and concurrent architectures even in a traditionally single-threaded language | ||
like PHP. With simple userland task scheduling systems interleaved generators become lightweight | like PHP. With simple userland task scheduling systems interleaved generators become lightweight | ||
Line 142: | Line 142: | ||
In the absence of generator return values, though, applications face an environment where " | In the absence of generator return values, though, applications face an environment where " | ||
- | tasks can be offloaded without a standardized way to return the eventual | + | tasks can be offloaded without a standardized way to return the eventual result. |
This is one reason why this proposal depends on the acceptance of the Generator Return Expressions RFC. | This is one reason why this proposal depends on the acceptance of the Generator Return Expressions RFC. | ||
The other reason return values are required stems from the previously discussed refactoring principle. | The other reason return values are required stems from the previously discussed refactoring principle. | ||
Specifically: | Specifically: | ||
- | like ordinary functions | + | like ordinary functions. |
Using the proposed syntax an ordinary function '' | Using the proposed syntax an ordinary function '' | ||
Line 164: | Line 164: | ||
simply by thinking of '' | simply by thinking of '' | ||
- | **NB:** The actual implementation of single-threaded concurrency | + | **NB:** The actual implementation of coroutine task schedulers is outside the scope of |
this document. This RFC focuses only on the language-level machinery needed to make such tools more | this document. This RFC focuses only on the language-level machinery needed to make such tools more | ||
feasible in userland. It should be obvious that simply moving code into a generator function will | feasible in userland. It should be obvious that simply moving code into a generator function will | ||
- | not instantly | + | not somehow |
Line 333: | Line 333: | ||
while($shared-> | while($shared-> | ||
$delegator = delegator($shared); | $delegator = delegator($shared); | ||
- | while($delegator-> | + | |
- | var_dump($delegator-> | + | foreach |
+ | var_dump($value); | ||
+ | } | ||
+ | var_dump($delegator-> | ||
+ | |||
+ | /* | ||
+ | int(42) | ||
+ | // This is our only output because no values are yielded | ||
+ | // from the already-completed shared subgenerator | ||
+ | */ | ||
</ | </ | ||
Line 366: | Line 375: | ||
/* | /* | ||
int(2); | int(2); | ||
+ | int(3); | ||
int(4); | int(4); | ||
int(42) | int(42) | ||
Line 373: | Line 383: | ||
==== Error States ==== | ==== Error States ==== | ||
- | There are two scenarios in which '' | + | There are two scenarios in which '' |
- | * Using '' | + | * Using '' |
- | + | ||
- | * Using '' | + | |
+ | * Using '' | ||
| | ||
===== Rejected Ideas ===== | ===== Rejected Ideas ===== | ||
- | The original version of this RFC proposed a '' | + | The original version of this RFC proposed a '' |
- | '' | + | '' |
- | form was considered | + | form was considered less readable |
Line 406: | Line 415: | ||
**Python** | **Python** | ||
- | Python 3.3 generators support '' | + | Python 3.3 generators support |
<code python> | <code python> | ||
- | >>> | + | >>> |
- | ... yield from range(x, | + | ... tally = 0 |
- | ... | + | ... while 1: |
+ | ... next = yield | ||
+ | ... if next is None: | ||
+ | ... | ||
+ | ... tally += next | ||
... | ... | ||
- | >>> | + | >>> |
- | [5, 4, 3, 2, 1, 0, 1, 2, 3, 4] | + | ... while 1: |
+ | ... tally = yield from accumulate() | ||
+ | ... | ||
+ | ... | ||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | ... | ||
+ | ... | ||
+ | >>> | ||
+ | >>> | ||
+ | ... | ||
+ | ... | ||
+ | >>> | ||
+ | >>> | ||
+ | [6, 10] | ||
</ | </ | ||
**JavaScript** | **JavaScript** | ||
- | Javascript ES6 generators support the '' | + | Javascript ES6 generators support the '' |
<code javascript> | <code javascript> | ||
- | function* | + | function* |
- | yield 2; | + | yield* [1, 2, 3]; |
- | yield 3; | + | |
- | | + | |
} | } | ||
- | function* | + | var result; |
- | | + | |
- | | + | function* |
- | yield 5; | + | |
} | } | ||
- | var iterator = g2(); | + | var iterator = g5(); |
console.log(iterator.next()); | console.log(iterator.next()); | ||
console.log(iterator.next()); | console.log(iterator.next()); | ||
console.log(iterator.next()); | console.log(iterator.next()); | ||
- | console.log(iterator.next()); | + | console.log(iterator.next()); |
- | console.log(iterator.next()); // { value: | + | // g4() returned |
- | console.log(iterator.next()); // { value: undefined, done: true } | + | |
+ | console.log(result); // " | ||
</ | </ | ||
Line 457: | Line 486: | ||
- | ===== Proposed Voting Choices | + | ===== Vote ===== |
- | + | ||
- | Two voting choices are proposed†: | + | |
- | * **YES**, | + | A 2/3 " |
- | * **NO**, do not modify existing Generator behavior | + | <doodle title=" |
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
- | A 2/3 " | + | . |
- | †: The success of this vote depends on the success of the accompanying [[rfc: | + | The success of this vote depends on the success of the accompanying [[rfc: |
- | Should Generator Return Expressions be rejected the voting outcome of this RFC will be rendered moot. | + | |
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
- | The current patch is approaching | + | The current patch is considered |
https:// | https:// |
rfc/generator-delegation.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1