# [RFC][Discussion] iterable_to_array() and iterable_count()

- Thread: https://externals.io/message/102313
- Original archive: https://news-web.php.net/php.internals/102313
- Started by Michael Moravec on 2018-06-20T01:55:02+00:00
- Messages: 3
- RFC: iterable_to_array() and iterable_count() (Declined) — https://externals.io/rfc/iterable_to_array-and-iterable_count.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #102313 — Michael Moravec — 2018-06-20T01:55:02+00:00

URL: https://externals.io/message/102313#102313

Hello internals,

I'd like to propose two new functions for PHP 7.3: iterable_to_array() and
iterable_count()

These functions are supposed to work with iterables (7.1 pseudotype) - both
arrays and iterators, unlike iterator_*() functions which only work with
iterators.

Here's an RFC for this:
https://wiki.php.net/rfc/iterable_to_array-and-iterable_count

Note that in order to make iterable_to_array() compatible with
 `iterator_to_array()`, `$use_keys = true` behavior is retained. Although this
is probably not a good default value as it may cause data loss, using
different default value sounds like a way worse option in terms of
consistency and compatibility.

A PR with implementation for iterable_to_array() is already submitted,
iterable_count() to be added soon.

Looking forward for your feedback.

Thanks!
Michael Moravec

---

## #102331 — johannes@schlueters.de — 2018-06-20T16:40:36+00:00

URL: https://externals.io/message/102313#102331 · In reply to: #102313

> Hello internals,
> 
> I'd like to propose two new functions for PHP 7.3:
> iterable_to_array() and
> iterable_count()
> 
> These functions are supposed to work with iterables (7.1 pseudotype)
> - both arrays and iterators, unlike iterator_*() functions which only
> work with iterators.
> 

Is there any reason not to extend the existing functions to also allow
arrays?

Also for the count one: Mind that `iterator_count()`/iterable_count()
doesn't respect the Countable interface and consumes the iterator,
which might not be resetable. A slightly better choice might be

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

And even then I would put a warning sign against blindly using it on
any iterator.

johannes

---

## #102552 — Michael Moravec — 2018-07-02T02:14:02+00:00

URL: https://externals.io/message/102313#102552 · In reply to: #102331

Hi Johannes,

2018-06-20 18:40 GMT+02:00 Johannes Schlüter <johannes@schlueters.de>:

>
> Is there any reason not to extend the existing functions to also allow
> arrays?
>

Yes, this was discussed in the PR on GitHub:
https://github.com/php/php-src/pull/3293#issuecomment-397082988

There are two main reasons to not do that:
1) Naming: iterator_*(), as the name suggests, is supposed to work with
iterators.
    Making it work with arrays would be highly confusing.
2) Not altering/reusing existing functions: Changing behavior of existing
functions
    that exist for many years is also confusing for consumers.
In both cases this would make code harder to understand (i.e. taking version
of PHP into efffect).


> Also for the count one: Mind that `iterator_count()`/iterable_count()
> doesn't respect the Countable interface and consumes the iterator,
> which might not be resetable. A slightly better choice might be
>
>     (is_array($iterable) || implements_countable($iterable)) ?
>           count($iterable) : iterator_count($iterable)
>
> And even then I would put a warning sign against blindly using it on
> any iterator.
>

Just like with existing `iterator_count()`, this is up to the consumer to
decide
whether it's safe or not to use (especially with generators). It has exactly
the same benefits and drawbacks.

Also I'm not sure whether this is a downside of *_count() or not, IMHO it's
designed specifically to count an iterator that is not Countable.
So for an iterable, one may prefer:
    is_countable($value) ? count($value) : iterable_count($value)
But again, this is up to the consumer to decide whether it's safe or not.


johannes
>


Thanks,
Michael
