rfc:iterator_xyz_accept_array

Differences

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

Link to this comparison view

Next revision
Previous revision
rfc:iterator_xyz_accept_array [2022/06/21 14:10] – created timwollarfc:iterator_xyz_accept_array [2022/07/19 14:47] (current) – Implemented. timwolla
Line 3: Line 3:
   * Date: 2022-06-21   * Date: 2022-06-21
   * Author: Tim Düsterhus, duesterhus@woltlab.com   * Author: Tim Düsterhus, duesterhus@woltlab.com
-  * Status: Draft+  * Status: Implemented 
 +  * Target Version: PHP 8.2 
 +  * Implementation: https://github.com/php/php-src/commit/7ae7df5b4601f3f0ce45a27324fb9c6ebcbfc9ed
   * First Published at: http://wiki.php.net/rfc/iterator_xyz_accept_array   * First Published at: http://wiki.php.net/rfc/iterator_xyz_accept_array
  
Line 9: Line 11:
 ===== Introduction ===== ===== Introduction =====
  
-PHP's iterator_*() family currently only accept <php>\Traversable</php>s (i.e. they reject plain <php>array</php>s). This is unnecessarily limiting.+PHP'<php>iterator_*()</php> family currently only accept <php>\Traversable</php>s (i.e. they reject plain <php>array</php>s). This is unnecessarily limiting.
  
-Specifically this concerns the <php>iterator_to_array()</php><php>iterator_count()</php>, and <php>iterator_apply()</php> function. While each of them has an array-specific counterpart, the fact that one needs to choose either the array-specific variant or the everything-but-array variant makes writing code the deals with arbitrary <php>iterable</php>s unnecessarily verbose.+Specifically this concerns the <php>iterator_to_array()</php> and <php>iterator_count()</php> functions. While each of them has an array-specific counterpart, the fact that one needs to choose either the array-specific variant or the everything-but-array variant makes writing code the deals with arbitrary <php>iterable</php>s unnecessarily verbose. 
 + 
 +As an example: Allowing <php>iterator_to_array()</php> to take an array, makes it much easier to write functions accepting an <php>iterable</php> and processing it using <php>array_map()</php> et al: 
 + 
 +<PHP> 
 +function before(iterable $foo) { 
 +    if (!is_array($foo)) { 
 +        $foo = iterator_to_array($foo); 
 +    } 
 + 
 +    return array_map(strlen(...), $foo); 
 +
 +function after(iterable $foo) { 
 +    $foo = iterator_to_array($foo); 
 + 
 +    return array_map(strlen(...), $foo); 
 +
 +</PHP>
  
 ===== Proposal ===== ===== Proposal =====
  
-The <php>$iterator</php> parameter of <php>iterator_to_array()</php><php>iterator_count()</php>, and <php>iterator_apply()</php> should be widened from <php>\Traversable</php> to <php>iterable</php> (i.e. to <php>\Traversable|array</php>).+The <php>$iterator</php> parameter of <php>iterator_to_array()</php> and <php>iterator_count()</php> should be widened from <php>\Traversable</php> to <php>iterable</php> (i.e. to <php>\Traversable|array</php>)
 + 
 +Specifically if this RFC is accepted the following shall hold: 
 + 
 +==== iterator_to_array ==== 
 + 
 +<PHP> 
 +iterator_to_array($array, true) == $array 
 +iterator_to_array($array, false) == array_values($array) 
 +</PHP> 
 + 
 +==== iterator_count ==== 
 + 
 +<PHP> 
 +iterator_count($array) == count($array) 
 +</PHP> 
 + 
 +==== iterator_apply ==== 
 + 
 +This function is **not** part of this proposal, because it is non-obvious how to define the behavior for <php>array</php>s, given that it does not pass the <php>Iterator</php> to the callback by default.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 53: Line 91:
 ===== Unaffected PHP Functionality ===== ===== Unaffected PHP Functionality =====
  
-Anything that isn't <php>iterator_to_array()</php><php>iterator_count()</php>, or <php>iterator_apply()</php>.+Anything that isn't <php>iterator_to_array()</php> or <php>iterator_count()</php>.
  
 ===== Future Scope ===== ===== Future Scope =====
Line 61: Line 99:
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
  
-Each vote requires a 2/3 majority..+Each vote requires a 2/3 majority. 
 + 
 +Voting opened 2022-07-05 14:30 UTC and closes on 2022-07-19 14:45 UTC. 
 + 
 +<doodle title="iterator_to_array: Change the type of iterator_to_array()’s $iterator parameter from \Traversable to iterable?" auth="timwolla" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle> 
 + 
  
-  * Vote 1: Change the type of <php>iterator_to_array()</php>'s <php>$iterator</php> parameter from <php>\Traversable</php> to <php>iterable</php>? +<doodle title="iterator_count: Change the type of iterator_count()s $iterator parameter from \Traversable to iterable?" auth="timwolla" voteType="single" closed="true"
-  * Vote 2: Change the type of <php>iterator_count()</php>'<php>$iterator</php> parameter from <php>\Traversable</php> to <php>iterable</php>? +   Yes 
-  Vote 3: Change the type of <php>iterator_apply()</php>'s <php>$iterator</php> parameter from <php>\Traversable</php> to <php>iterable</php>?+   * No 
 +</doodle>
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
  
-PoC implementation to <php>iterator_to_array</php>: https://github.com/php/php-src/pull/8819+https://github.com/php/php-src/pull/8819
  
 ===== Implementation ===== ===== Implementation =====
  
-n/a+https://github.com/php/php-src/commit/7ae7df5b4601f3f0ce45a27324fb9c6ebcbfc9ed
  
 ===== References ===== ===== References =====
Line 79: Line 127:
   * Pre-RFC discussion: https://externals.io/message/117979   * Pre-RFC discussion: https://externals.io/message/117979
   * PoC implementation: https://github.com/php/php-src/pull/8819   * PoC implementation: https://github.com/php/php-src/pull/8819
-  * Similar previous RFC that proposed adding **new** functions: https://wiki.php.net/rfc/iterable_to_array-and-iterable_count+  * Similar previous RFC that proposed adding **new** functions with an <php>iterable_*</php> prefix: https://wiki.php.net/rfc/iterable_to_array-and-iterable_count 
 +  * Stack Overflow asking for <php>iterable_to_array()</php>: https://stackoverflow.com/q/44587973/782822
  
 ===== Rejected Features ===== ===== Rejected Features =====
  
 none none
rfc/iterator_xyz_accept_array.1655820641.txt.gz · Last modified: 2022/06/21 14:10 by timwolla