Open
Description
Like the reverse() method in the following example:
// A generic definition, shortened for brevity
class List<E> {
public function __construct(E... $elements) { }
public function get(int $i): E $element { }
public function size(): int { }
}
class Test {
// Generic method declaration
private static function reverse<E>(List<E> $list): iterable {
for ($i= $list->size() - 1; $i >= 0; $i--) {
yield $list->get($i);
}
}
public static function main(array<string> $args): void {
$list= new List<string>($args);
// Call the generic method, potentially without type (as it could be inferred)
foreach (self::reverse<string>($list) as $element) {
Console::writeLine($element);
}
}
}