Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision |
rfc:any_all_on_iterable [2021/02/06 14:37] – tandre | rfc:any_all_on_iterable [2021/02/08 15:35] – tandre |
---|
====== PHP RFC: PHP\iterable\any() and all() on iterables ====== | ====== PHP RFC: PHP\iterable\any() and all() on iterables ====== |
* Version: 0.4 | * Version: 0.5 |
* Date: 2020-08-30 | * Date: 2020-08-30 |
* Author: Tyson Andre, tandre@php.net | * Author: Tyson Andre, tandre@php.net |
* Status: Under Discussion | * Status: Voting |
* First Published at: https://wiki.php.net/rfc/any_all_on_iterable | * First Published at: https://wiki.php.net/rfc/any_all_on_iterable |
* Implementation: https://github.com/php/php-src/pull/6053 | * Implementation: https://github.com/php/php-src/pull/6053 |
<code php> | <code php> |
// The old version | // The old version |
$satisifes_predicate = false; | $satisifies_predicate = false; |
foreach ($item_list as $item) { | foreach ($item_list as $item) { |
// Performs DB operations or external service requests, stops on first match by design. | // Performs DB operations or external service requests, stops on first match by design. |
===== Proposal ===== | ===== Proposal ===== |
| |
Add the functions ''PHP\iterable\any(iterable $input, ?callable $callback = null): bool'' and ''all(...)'' to php's standard function set. | Add the functions ''PHP\iterable\any(iterable $input, ?callable $callback = null): bool'' and ''all(...)'' to PHP's standard library's function set. |
(The namespace ''PHP\iterable'' was preferred in [[rfc:any_all_on_iterable_straw_poll_namespace#vote|a straw poll that was previously sent out]]) | (The namespace ''PHP\iterable'' was preferred in [[rfc:any_all_on_iterable_straw_poll_namespace#vote|a straw poll that was previously sent out]]) |
| |
| **The implementation is equivalent to the following polyfill:** |
| |
<code php> | <code php> |
Benefits of a shorter name: | Benefits of a shorter name: |
| |
- Conciseness for the most common use of ''any*'' and ''all*'' | - Conciseness for the most common use case of checking whether a predicate is true for any/all values of an iterable array/object. |
- Consistency with some other functions such as ''array_reduce()'', ''in_array()'', ''next()'' that act only on values | - Consistency with some other functions such as ''array_reduce()'', ''array_unique()'', ''in_array()'', ''next()'' that use values for their underlying implementation (i.e. not being named ''array_reduce_values()'', ''next_value()'', etc.) |
- Potential to use $flags to extend this to support less common use cases like ''ARRAY_FILTER_USE_KEY''/''ARRAY_FILTER_USE_BOTH'' without adding more global functions | - Potential to use $flags to extend this to support less common use cases like ''ARRAY_FILTER_USE_KEY''/''ARRAY_FILTER_USE_BOTH'' without adding more global functions |
| |
Benefits of a longer name: | Benefits of a longer name: |
| |
- Descriptiveness/harder to misread. | - A longer name would be more descriptive and make it easier to understand what code is doing. |
- More likely to add multiple functions such as ''any_key()''/(''any_entry'' or ''any_key_value'') in the future (which will be simpler to statically analyze or infer types for - in rare cases the argument $flags passed to ''array_filter($values, $callback, $flags)'' is an unknown dynamic value). \\ Note that adding the name ''any()'' for values of iterables doesn't prevent PHP from adding ''any_key()'' for checking keys of iterables in the future, either. | - This makes it likely that in the future for iterable functionality, PHP will add multiple functions such as ''any_key()''/(''any_entry'' or ''any_key_value'') instead of using $flags (which will be simpler to statically analyze or infer types for - in rare cases the argument $flags passed to ''array_filter($values, $callback, $flags)'' is an unknown dynamic value). \\ Adding a constant such as ''flags: PHP\iterable\USE_KEY'' may make the code longer. \\ Note that adding the name ''any()'' for values of iterables doesn't prevent PHP from adding ''any_key()'' for checking keys of iterables in the future, either (my personal preference would be to add ''any_key()'' regardless of whether ''any()''/''any_value()'' was added). |
| |
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== |
| |
| |
===== Proposed Voting Choices ===== | ===== Vote ===== |
| |
| Add ''PHP\iterable\any(iterable $input, ?callable $callback = null): bool'' and ''PHP\iterable\all(iterable $input, ?callable $callback = null): bool'' (yes/no, requiring a 2/3 majority) |
| |
| Voting started on 2021-02-08 and ends on 2021-02-22. |
| |
| <doodle title="Add PHP\iterable\any() and all() to PHP?" voteType="single" auth="tandre" closed="false"> |
| * Yes |
| * No |
| </doodle> |
| |
| \\ The following secondary vote will be used to decide between ''any()''/''all()'' and ''any_value()''/''all_values()'' as the name within the ''PHP\iterable'' namespace. See [[#secondary_voteanyall_or_any_valueall_values|the discussion section for the benefits/drawbacks of those names]]. |
| |
| <doodle title="Names to use: any()/all() or any_value()/all_values()" voteType="single" auth="tandre" closed="false"> |
| * any()/all() |
| * any_value()/all_values() |
| </doodle> |
| |
Add ''PHP\iterable\any(iterable $input, ?callable $callback = null)'' and ''PHP\iterable\all(...)'' (yes/no, requiring 2/3 majority) | ==== Straw Poll ==== |
| |
A secondary vote requiring a simple majority will be used to decide between ''any()''/''all()'' and ''any_value()''/''all_values()'' as the name within the ''PHP\iterable'' namespace. See [[#secondary_voteanyall_or_any_valueall_values|the discussion section for the benefits/drawbacks of those names]]. | <doodle title="Reasons for voting against this RFC" voteType="multi" auth="tandre" closed="false"> |
| * Too small in scope |
| * Object to the choice of namespace |
| * Prefer the global namespace |
| * Confused about the implementation |
| * Prefer userland solutions |
| * Other |
| * Voted for this RFC |
| </doodle> |
| |
===== References ===== | ===== References ===== |
===== Changelog ====== | ===== Changelog ====== |
| |
0.3: Add more quotes | * 0.3: Add more quotes |
0.4: Change name to ''PHP\iterable\all'' and ''PHP\iterable\any'' | * 0.4: Change name to ''PHP\iterable\all'' and ''PHP\iterable\any'', add a secondary vote on ''any/all'' vs ''any_value()/all_values()'' |
| * 0.5: Add straw poll |