Generator return type must be a supertype of Generator

Description

When a method or a function uses a yield keyword, it becomes a generator. It also means that the return type of this method must be Generator, or any other of its supertypes: Traversable, Iterator, Generator or iterable.

There is no way to set a type on the yielded values: only on the returned values. In this case, it will be a Generator. Strangely, any value in a return expression is also valid.

It may also be simply removed, and left implicit.

Example

<?php

function foo() : X {
     yield 1;

     return true;
}

?>

Solutions

  • Use the return type Generator.