rfc:iterable_to_array-and-iterable_count

PHP RFC: iterable_to_array() and iterable_count()

Introduction

PHP 7.1 added an iterable pseudo-type which is a union of array and Traversable. Although iterable is very useful, sometimes it's necessary to convert it to an array (i.e. for array_*() functions or for compatibility with older code).

Proposal

PHP currently has iterator_to_array(), iterator_count() and iterator_apply(). Unfortunately these functions are incompatible with iterable as they only work with iterators, not arrays.

In order to transform an iterable to an array, one has to write code as such:

is_array($iterable) ? $iterable : iterator_to_array($iterable)
is_array($iterable) ? count($iterable) : iterator_count($iterable)

Besides being error-prone, this code makes iterables much harder to work with.

In order to improve user experience and ease adoption of iterables, two new functions should help mitigate the code above, basically expanding behavior of iterator_to_array() and iterator_count() to work also with arrays.

iterable_to_array()

The following code:

is_array($iterable) ? $iterable : iterator_to_array($iterable)

Is now replaced by simple:

iterable_to_array($iterable)

This function's behavior is similar to iterator_to_array():

  • when an array is given:
    • when $use_keys = true: array is returned as-is, unchanged
    • when $use_keys = false: array is returned as a list, similar to array_values()
  • when an iterator is given:
    • when $use_keys = true: iterator is converted to an array, preserving the keys (same behavior as iterator_to_array())
    • when $use_keys = false: iterator is converted to an array as a list, ignoring the keys (same behavior as iterator_to_array())

In order to stay consistent with iterator_to_array(), the $use_keys behavior is retained.

iterable_count()

The following code:

is_array($iterable) ? count($iterable) : iterator_count($iterable)

Is now replaced by simple:

iterable_count($iterable)

This function's behavior is similar to iterator_count():

  • when an array is given: returns the number of elements contained in the array (same as count())
  • when an iterator is given: returns the number of elements contained in the iterator (same as iterator_count())

Backward Incompatible Changes

None.

Proposed PHP Version(s)

next 7.x

RFC Impact

Two new SPL functions which could collide with user-land functions. Since iterable type is still quite new, the risk should be relatively low.

To SAPIs

None.

To Existing Extensions

None.

To Opcache

None.

Future Scope

Since these new functions provide a superset of features provided by iterator_to_array() and iterator_count(), these could be deprecated later on.

In case these functions gain a popularity, they could be optimized directly by VM, similar to ZEND_COUNT.

Vote

This is not a language change so simple yes/no vote with 50%+1 majority is required.

Voting starts on 2018-07-03 20:30 UTC and closes on 2018-07-16 23:00 UTC.

Add iterable_to_array()?
Real name Yes No
ajf  Image
ashnazg  Image
bwoebi  Image
dams Image 
dmitry  Image
galvao Image 
gasolwu  Image
hywan Image 
irker  Image
kalle  Image
kelunik  Image
krakjoe  Image
lcobucci Image 
lex  Image
marcio  Image
mcmic Image 
mrook  Image
narf  Image
nikic  Image
ocramius Image 
peehaa  Image
pmmaga  Image
pollita  Image
rtheunissen  Image
salathe  Image
seld Image 
subjective  Image
svpernova09  Image
trowski  Image
weierophinney  Image
yunosh Image 
zimt  Image
Final result: 8 24
This poll has been closed.

.

Add iterable_count()?
Real name Yes No
ajf  Image
ashnazg  Image
bwoebi  Image
cmb  Image
colinodell  Image
dams Image 
dmitry  Image
galvao Image 
gasolwu  Image
gooh  Image
hywan  Image
irker  Image
jhdxr  Image
kalle  Image
kelunik  Image
krakjoe  Image
levim  Image
lex  Image
marcio  Image
mcmic  Image
mgocobachi  Image
mrook  Image
narf  Image
nikic  Image
ocramius  Image
peehaa  Image
pmmaga  Image
pollita  Image
rtheunissen  Image
salathe  Image
seld  Image
subjective  Image
svpernova09  Image
trowski  Image
weierophinney  Image
yunosh  Image
zimt  Image
Final result: 2 35
This poll has been closed.

Patches and Tests

rfc/iterable_to_array-and-iterable_count.txt · Last modified: by 127.0.0.1

Image