Stealth GeneratorΒΆ

By Frederic Bouchery

The code below has a useless loop. The presence of the yield keyword in the function body makes it a generator. As such, foreach() will react to yield calls, though the function returns immediately, without a yield. Hence, the empty loop, even though the function returns an array: indeed, to have the function behave as expected, it is necessary to remove the unreachable yield call, and then, the foreach() can use the return for the loop.

../_images/stealth-generator.png