Bug #72499 [Nab]: Generator count always showing 1, seems to not be countable
| From: | kjarli at gmail dot com | Date: | Mon, 27 Jun 2016 06:40:12 +0000 |
| Subject: | Bug #72499 [Nab]: Generator count always showing 1, seems to not be countable | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-201862@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=72499&edit=1
ID: 72499
User updated by: kjarli at gmail dot com
Reported by: kjarli at gmail dot com
Summary: Generator count always showing 1, seems to not be
countable
Status: Not a bug
Type: Bug
Package: Scripting Engine problem
Operating System: 3v4l.org
PHP Version: 5.5.37
Block user comment: N
Private report: N
New Comment:
Alright, if it's working as intended, this can be closed. Thanks for the update!
Previous Comments:
------------------------------------------------------------------------
[2016-06-27 06:36:18] requinix@php.net
Generator cannot implement Countable because that would require knowing how many values the function
will eventually yield, which would require that it executes the function until completion, which is
often not possible (consider the case when values are yielded into the function). Besides, it's
the opposite of what generators are supposed to be used for.
If you, as the developer, know that it would be safe to get a count in this way then you can use
iterator_to_array() - or a foreach with a counter to avoid the memory usage. But at that point you
should reconsider using a generator in the first place.
------------------------------------------------------------------------
[2016-06-27 06:21:01] kjarli at gmail dot com
Description:
------------
When I use count() on iterators or arrays for example, I always get the number. For Generators, I
will have to call array_to_iterator first. I'm not too familiar with how this is supposed to
work so I was wondering if this was a bug or not.
If this is not a bug, feel free to close it.
Test script:
---------------
https://3v4l.org/uscsN
Expected result:
----------------
I would expect the Generator to implement the Countable interface and call iterator_to_array
internally or something.
Actual result:
--------------
see link
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=72499&edit=1