Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision |
rfc:iterator_xyz_accept_array [2022/06/21 14:21] – timwolla | rfc:iterator_xyz_accept_array [2022/07/19 14:45] – Accepted timwolla |
---|
* 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: Accepted |
* First Published at: http://wiki.php.net/rfc/iterator_xyz_accept_array | * First Published at: http://wiki.php.net/rfc/iterator_xyz_accept_array |
| |
PHP's <php>iterator_*()</php> family currently only accept <php>\Traversable</php>s (i.e. they reject plain <php>array</php>s). This is unnecessarily limiting. | PHP's <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> 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. | 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. |
| |
Specifically 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: | 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> | <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>). | 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>). |
| |
====== iterator_to_array ====== | Specifically if this RFC is accepted the following shall hold: |
| |
| ==== iterator_to_array ==== |
| |
<PHP> | <PHP> |
</PHP> | </PHP> |
| |
====== iterator_count ====== | ==== iterator_count ==== |
| |
<PHP> | <PHP> |
</PHP> | </PHP> |
| |
====== iterator_apply ====== | ==== 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. | 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. |
===== 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>'s <php>$iterator</php> parameter from <php>\Traversable</php> to <php>iterable</php>? | * Yes |
| * 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 ===== |