2 <h2>yield and generator</h2>
4 This feature requires the [[index.php?file=options.compiler | compiler option]]
5 EnableHipHopSyntax=true, or interpreter option Eval.EnableHipHopSyntax=true.
7 HipHop extends PHP to include Python and C#-style generators. If you're
8 unfamiliar with the concept, see
9 [[http://docs.python.org/tutorial/classes.html#generators | the Python docs]].
10 As in Python, the <i>yield</i> keyword marks the enclosing function as a
20 foreach (foo() as $a) {
24 The above program outputs "123,456,". To abort a generator sequence, use "yield
29 // this will stop the "foreach" immediately without any value returned
30 if ($abort) yield break;
36 Generators must observe the following restrictions:
38 - Generators are <b>not recursive</b>. In the above example, foo() cannot call
39 foo() while iterating.
40 - Generators are <b>called once</b>: foo() cannot be called again after it's
42 - Do not call the rewind() method of the objects (of class Iterator) returned by
45 Also, yield in HipHop also supports passing a value from outside of the
53 From outside the generator, instead of resuming the generator with
54 Continuation::next(), one can call Continuation::send() to pass a value, which
55 will be assigned to $a, back into the generator.
57 Note that the yield expression in the above example is not really an expression;
58 it can only appear on its own on the RHS of an assignment statement. This is to
59 avoid the complicated evaluation order problem in bizarre expressions like
60 "($a = yield 5) * (yield $a + 3) - ($a = yield 4)".