Coroutines as Data Types

Coroutines are a type of function that can be paused and resumed. They are similar to lua coroutines.

Coroutines are built-in data types, their usage is almost identical to regular functions, except that they can be accessed as objects (they are not object per se!)

Declaration

Once a coroutine returns, it will need to be manually called again to continue execution. It can return values or no values at all.

Life Cycle

Coroutines can have the following state:

  • created: The coroutine is created but not yet executed.
  • running: The coroutine is running.
  • suspended: The coroutine is suspended.
  • dead: The coroutine is dead.

Example

Arguments

Coroutines can take arguments just like regular functions, these are argument may not change during the coroutine's execution.


Kudos! Keep reading!