When statement

Example:

  1. when sizeof(int) == 2:
  2. echo "running on a 16 bit system!"
  3. elif sizeof(int) == 4:
  4. echo "running on a 32 bit system!"
  5. elif sizeof(int) == 8:
  6. echo "running on a 64 bit system!"
  7. else:
  8. echo "cannot happen!"

The when statement is almost identical to the if statement with some exceptions:

  • Each condition (expr) has to be a constant expression (of type bool).
  • The statements do not open a new scope.
  • The statements that belong to the expression that evaluated to true are translated by the compiler, the other statements are not checked for semantics! However, each condition is checked for semantics.

The when statement enables conditional compilation techniques. As a special syntactic extension, the when construct is also available within object definitions.