I have a function that gives back a generator. At the moment it uses yield from:
function foo()
{
$generator = getGenerator();
// some other stuff (no yields!)
yield from $generator;
}
If I replace that yield from with a simple return, does that change anything in this case? Maybe in the execution? Or performance? Does yield from produces a new 'outer' iterator?
I know, in other cases yield from can be more flexible because I can use it several times and even mix it with simple yields, however that doesn't matter for my case.
When you use
yield fromonce at the end of the method like you do above, this does not change things for what is returned by the method. But there is still a difference for the code inside the method: any code before theyield fromis called when rewinding the iterator at the beginning of the iteration, while the code before thereturnis executed when calling the function, as this function is just a normal function now (actually, this is not specific toyield from. It is the behavior ofyield. See https://3v4l.org/jnPvC for the proof).The big advantage of
yield fromis that you can use it multiple times in the function, which combined values of all iterators into a single iterator.