Jay Bienvenu
Jay Bienvenu

Reputation: 3291

"yield" overwrites "yield from" items

<?php
function generator1(): \Traversable
{
    yield 'alpha';
    yield 'beta';
    yield 'delta';
}

function generator2(): \Traversable
{
    yield from generator1();
    yield 'upsilon';
    yield 'omega';
}

function resolve(iterable $generator) {
    foreach ($generator as $index => $item) 
        echo "$index $item" . PHP_EOL;
}

resolve(generator1());
echo  PHP_EOL;

resolve(generator2());
die;

Produces this result:

0 alpha
1 beta
2 delta

0 alpha
1 beta
2 delta
0 upsilon
1 omega

That last stream should be:

0 alpha
1 beta
2 delta
3 upsilon
4 omega

What's going on here? Is this a bug in PHP (I'm running v7.4.10 in Ubuntu.) How do I get the correct result without using an array or some other form of collector?

Upvotes: 4

Views: 129

Answers (1)

jspit
jspit

Reputation: 7703

The result is a specialty of the iterator_to_array() function. The second parameter must be set to false so that all values are recorded as already noted in the comment.

This is also specifically pointed out in the PHP manual.

function generator1(): \Traversable
{
    yield 'alpha';
    yield 'beta';
    yield 'delta';
    yield 'gamma';
    yield 'epsilon';
    yield 'eta';
}

function generator2(): \Traversable
{
    yield from generator1();
    yield 'upsilon';
    yield 'omega';
}

var_dump(iterator_to_array(generator2()));

var_dump(iterator_to_array(generator2(), false));

Result:

array(6) {
  [0]=>
  string(7) "upsilon"
  [1]=>
  string(5) "omega"
  [2]=>
  string(5) "delta"
  [3]=>
  string(5) "gamma"
  [4]=>
  string(7) "epsilon"
  [5]=>
  string(3) "eta"
}
array(8) {
  [0]=>
  string(5) "alpha"
  [1]=>
  string(4) "beta"
  [2]=>
  string(5) "delta"
  [3]=>
  string(5) "gamma"
  [4]=>
  string(7) "epsilon"
  [5]=>
  string(3) "eta"
  [6]=>
  string(7) "upsilon"
  [7]=>
  string(5) "omega"
}

Try for yourself in the sandbox.

This becomes clear when keys and values are output with foreach.

foreach(generator2() as $key => $value){
  var_dump($key,$value);
}

Output:

int(0) string(5) "alpha"
int(1) string(4) "beta"
int(2) string(5) "delta"
int(3) string(5) "gamma"
int(4) string(7) "epsilon"
int(5) string(3) "eta"
int(0) string(7) "upsilon"
int(1) string(5) "omega"

Upvotes: 3

Related Questions