duanji1043 2016-08-04 20:16
浏览 51

作为协程的yield(rvalue)的PHP文档

You can create a generator in PHP that yields values:

function genOneTwoThree() {
  foreach ([1,2,3] as $i => $number)
    yield $i => $number;
}

Then you can consume them like:

foreach (genOneTwoThree() as $i) {
  echo $i;
}

This "lvalue" usage is documented here: https://secure.php.net/manual/en/language.generators.syntax.php#control-structures.yield

But yield can also be used as an rvalue like this:

function printer() {
  while (true) {
    $string = yield;
    echo $string;
  }
}
$printer = printer();
$printer->send('Hello world!');
$printer->send('Bye world!');

This "consumer" example is given in the "Generator" documentation at https://secure.php.net/manual/en/generator.send.php

Is there another place that actually documents this type of usage? Some other specific question I have are:

  • Can rvalue yield functions return something, and when does that happen?
  • What happens when rvalue yield functions affect global state
  • Can you use rvalue and lvalue yield to filter a generator?
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类
    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了