我正在玩一些 Hack 并尝试使用yield
关键字创建生成器函数。该文档指出,此类函数的返回类型应该是Continuation
接口。但是,在hh_client
生成器函数的源代码示例上运行时,我得到以下输出:
./test.php:4:3,7: 无效产量 (Typing[4110])
./test.php:3:17,28: 这是一个 Continuation 类型的对象
./test.php:4:3,7 :它与 Generator 类型的对象不兼容(主体中带有“yield”的函数的结果)
这是test.php:
<?hh
function gen(): Generator<int> {
yield 1;
yield 2;
yield 3;
}
function foo(): void {
foreach (gen() as $x) {
echo $x, "\n";
}
}
foo();
更改结果类型以Generator
提供更多警告。输入生成器函数的正确方法是什么?