E02060: duplicated enumerated value in switch 'case'

Category: E02xxx Symbol definition · Reported in phase: FULL_RESOLUTION · Classification: DUPLICATE_ENUMERATED_VALUES_PRESENT_IN_SWITCH · All error codes

What went wrong

The same enumeration value appears in multiple 'case' clauses of a switch. Each value can only be handled by one case.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 99 (How do I create an enumeration in EK9?)

How to fix it

  • Remove the duplicate case
  • Use comma-separated values for shared handling: case Red, Green

See also

  • E02060: Duplicate values in switch CASE clauses
  • E01050: Duplicate values in enum TYPE definition
  • E07310: Missing enumeration values in switch (exhaustiveness)

Examples

Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.

Fails with E02060: duplicated enumerated value in switch 'case'

#!ek9
defines module error.e02060.bad

  defines type

    Direction
      North
      South
      East
      West

  defines function

    describe()
      -> direction as Direction
      <- rtn as String?

      switch direction
        case Direction.North
          rtn: "Going North"
        case Direction.South, Direction.East
          rtn: "Going South or East"
        case Direction.West
          rtn: "Going West"
        case Direction.North
          rtn: "North again"
        case Direction.East
          rtn: "East is already in the combined case above"
        default
          rtn: "Unknown"

Fixed: Each enumerated value in exactly one case; every value covered, and a default

#!ek9
defines module error.e02060.good

  defines type

    Direction
      North
      South
      East
      West

  defines function

    describe()
      -> direction as Direction
      <- rtn as String?

      switch direction
        case Direction.North
          rtn: "Going North"
        case Direction.South, Direction.East
          rtn: "Going South or East"
        case Direction.West
          rtn: "Going West"
        default
          rtn: "Unknown"

Questions and answers

These Q&A entries show E02060 and its fix: