Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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;
}

?>

Alternatives

  • Use the return type Generator.