Switching over an enumeration

The complete source of flowControl_enum_switch.ek9, compiled by the EK9 compiler. Shown on flowControl.html.

#!ek9
defines module site.flowControl.enumswitch

  defines type

    <?-
      This is the enumeration that is used in the example below.
      It is finite in the number of values it can hold.
    -?>
    LimitedEnum
      A,
      B,
      C,
      D

  defines function

    SimpleExhaustiveEnumerationExpressionSwitch()
      -> selection as LimitedEnum

      result <- switch selection
        <- rtn as String?
        case LimitedEnum.A
          rtn: "Just A"
        case LimitedEnum.B
          rtn: "Just B"
        case LimitedEnum.C
          rtn: "Just C"
        case LimitedEnum.D
          rtn: "Just D"
        default
          rtn: "Selection is not set"

      require result?

  defines function

    SimpleNonExhaustiveEnumerationExpressionSwitch()
      -> selection as LimitedEnum

      result <- switch selection
        <- rtn as String?
        case == LimitedEnum.A
          rtn: "Just A"
        default
          rtn: "Selection is anything but A"

      require result?

Download flowControl_enum_switch.ek9