rfc:foreach_void
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
rfc:foreach_void [2016/01/09 22:30] – created jbafford | rfc:foreach_void [2021/03/27 14:53] (current) – Move to inactive ilutov | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: foreach iteration of keys with no values ====== | + | ====== PHP RFC: foreach iteration of keys without |
* Version: 0.1 | * Version: 0.1 | ||
* Date: 2016-01-09 | * Date: 2016-01-09 | ||
* Author: John Bafford, john@bafford.com | * Author: John Bafford, john@bafford.com | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | When iterating | + | When iterating with foreach, it is not possible to retrieve only the keys (and not also the values) |
===== Background ===== | ===== Background ===== | ||
Line 14: | Line 14: | ||
<code php> | <code php> | ||
- | foreach ($array as $key => $value) { | + | foreach ($source |
// use the $key, ignore the $value | // use the $key, ignore the $value | ||
} | } | ||
</ | </ | ||
- | Retrieving the array value and ignoring it has a performance cost in that an opcode is emitted in compilation (and executed during iteration) to retrieve the value at each iteration. If the $value is never actually used, this is an unnecessary cost. | + | Retrieving the $value and ignoring it has a performance cost in that an opcode is emitted in compilation (and executed during iteration) to retrieve the value at each iteration. If the $value is never actually used, this is an unnecessary cost. |
- | Additionally, | + | Additionally, |
Line 30: | Line 30: | ||
Retrieving the list of array keys for iteration in this manner is more semantically pure because it clearly indicates intent, but it involves a performance cost as a result of the call to array_keys. (An additional traversal of the array, as well as the time and memory cost of allocating/ | Retrieving the list of array keys for iteration in this manner is more semantically pure because it clearly indicates intent, but it involves a performance cost as a result of the call to array_keys. (An additional traversal of the array, as well as the time and memory cost of allocating/ | ||
+ | |||
===== Proposal ===== | ===== Proposal ===== | ||
- | In order to iterate over an array and retrieve only its keys, we add the following syntax: | + | In order to iterate over an array (or generator, or other object that can be iterated) |
<code php> | <code php> | ||
- | foreach ($array as $key => void) { | + | foreach ($source |
} | } | ||
</ | </ | ||
- | By adding | + | By adding |
By not having a target variable that receives the value, an opcode is no longer emitted (or executed) to copy that value from the source. This provides a minor performance enhancement at runtime at the expense of a slightly more complicated parser and compiler for foreach. | By not having a target variable that receives the value, an opcode is no longer emitted (or executed) to copy that value from the source. This provides a minor performance enhancement at runtime at the expense of a slightly more complicated parser and compiler for foreach. | ||
Line 52: | Line 53: | ||
While in draft, the direct link to the relevant tree is: https:// | While in draft, the direct link to the relevant tree is: https:// | ||
- | |||
- | At present, this is a prototype, pending review. | ||
Line 71: | Line 70: | ||
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
- | * Whether to add '' | + | * Whether to add '' |
This proposal adds new syntax. Accordingly, | This proposal adds new syntax. Accordingly, |
rfc/foreach_void.1452378631.txt.gz · Last modified: 2017/09/22 13:28 (external edit)