rfc:foreach_void

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
rfc:foreach_void [2016/01/09 22:30] – created jbaffordrfc: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 values ======
   * 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: Draft (or Under Discussion or Accepted or Declined)+  * Status: Inactive
   * First Published at: http://wiki.php.net/rfc/foreach_void   * First Published at: http://wiki.php.net/rfc/foreach_void
  
 ===== Introduction ===== ===== Introduction =====
-When iterating over an array with foreach, it is not possible to retrieve only the array keys without unnecessary extra work or semantic overhead. This RFC proposes new syntax that makes this possible.+When iterating with foreach, it is not possible to retrieve only the keys (and not also the values) without unnecessary extra work or semantic overhead. This RFC proposes new syntax that makes this possible.
  
 ===== Background ===== ===== Background =====
Line 14: Line 14:
  
 <code php> <code php>
-foreach ($array as $key => $value) {+foreach ($source as $key => $value) {
     // use the $key, ignore the $value     // use the $key, ignore the $value
 } }
 </code> </code>
  
-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, there is a semantic cost, in that even though the $value is intended to be unused, it still exists. There is no clear indication strictly via code that the value is intended to be ignored.+Additionally, there is a semantic cost, in that even though the $value is intended to be unused, it still exists. There is no clear indication strictly via code that the value is intended to be unused. Even when the variable is named accordingly, such as $unused, $dummy, $ignore, $_, etc, it is not always clear as to the intent. For example, $unused, $dummy, and $ignore could be all be an accurate representation of the meaning of an array's values.
  
  
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/deallocating a new array to receive copies of the keys from the source array.) 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/deallocating a new array to receive copies of the keys from the source array.)
 +
  
 ===== 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) and retrieve only its keys, we add the following syntax:
  
 <code php> <code php>
-foreach ($array as $key => void) {+foreach ($source as $key => void) {
 } }
 </code> </code>
  
  
-By adding ''foreach ($array as $key => void)'', we gain the ability to iterate over the array’s keys in a performant and semantically appropriate manner, while still making it relatively easy to retrieve the array values should they be needed (either by replacing void with a variable definition, or by actually accessing the value from the source array using the key retrieved in iteration).+By adding this syntax, we gain the ability to iterate over the keys in a performant and semantically appropriate manner.
  
 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://github.com/jbafford/php-src/tree/foreachvoid While in draft, the direct link to the relevant tree is: https://github.com/jbafford/php-src/tree/foreachvoid
- 
-At present, this is a prototype, pending review. 
  
  
Line 71: Line 70:
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
-* Whether to add ''foreach ($array as $key => void)'' syntax+* Whether to add ''foreach ($source as $key => void)'' syntax
  
 This proposal adds new syntax. Accordingly, it requires a 2/3 vote. This proposal adds new syntax. Accordingly, it requires a 2/3 vote.
rfc/foreach_void.1452378631.txt.gz · Last modified: 2017/09/22 13:28 (external edit)