Anvil | Smiths Smiths | Register Register | Login Login |
Search:
Show links Show tools Show tree | Previous document Next document | njet.org > Anvil > Documentation > Language reference > Statements > Definition statements > Generator functions

Generator functions

If function (or method) contains one or more yield statements it is called generator. When called, generator function does not evalute its statements, instead, it returns generator enumeration of type anvil.lang.enumeration. The body of generator function is executed when ``next()ยดยด method is called or, in general, when more elements is retrieved from enumeration.

Execution continues until one of the three conditions are met

  • Yield statement is encountered: its contained expression is evaluated and returned as a next value from enumeration. State of generator is frozen; local variables and parameters are saved and when next element is retrieved execution continues right after the yield statement.
  • Return statement is encountered: return value is discarded and generator terminates and will produce no more values.
  • End of function is reached: Generator terminates and will produce no more values.

Only one thread at a time may call generator.

See also  | Yield
Contributes notes:
Add a note
What's new | Anvil