Yeah, sorry, it was a bad example -- I just noted after I submit. hehe About "new Generator($arg)" I mean a method to flag the first argument on function, that should identify that it should work differently or default method (without allocate memory). I guess that it is not implemented on PHP core, because until now have no sense, but imagine something like that (I will write in PHP, but read it like a C++):
PHP: new Generator(range(1, 1000)) C++ (range function): if (getParentConstructor() === Generator::class) { // Yield each one. } else { // Prepare on memory and return like an array. } In this case, when you call range() inside a Generator() constructor, then the range() (in C++) will acts differently, like a xrange(). 2017-03-17 22:57 GMT-03:00 Sara Golemon <poll...@php.net>: > On Fri, Mar 17, 2017 at 8:40 PM, David Rodrigues <david.pro...@gmail.com> > wrote: > > There are some way to PHP be flagged before run some function, and > > understand that you wants to return a Generator instead of array? > > For instance: new Generator(range(1, 1000)) makes range() work like a > > generator instead of return an array - it's an alternative to avoid new > > keywords. > > Or, maybe, some side implementation like Generator::range(1, 1000), > > Generator::array_values($array), etc. (Currently, for instance, we can > > simulate the range() Generator, but not array_values()) > > > Why would array_values() as a generator be difficult? > > function generate_values(Traversable $in) { > foreach ($in as $k => $v) { > yield $k => $v; > } > } > > In general, your `new Generator($arr)` concept misses most of the > usefulness of using a generator since arrays are already traversable, > and you've already allocated all the space of a full container. > Where's the benefit from making it a generator? > > -Sara > -- David Rodrigues