Re: [RFC][Discussion] iterable_to_array() and iterable_count()
| From: | Johannes Schlüter | Date: | Wed, 20 Jun 2018 16:40:36 +0000 |
| Subject: | Re: [RFC][Discussion] iterable_to_array() and iterable_count() | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-102331@lists.php.net to get a copy of this message | ||
On Mi, 2018-06-20 at 03:55 +0200, Michael Moravec wrote:
> 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