E07630: operator cannot be used on an Enumeration in this way

Category: E07xxx Full resolution · Reported in phase: FULL_RESOLUTION · Classification: OPERATOR_CANNOT_BE_USED_ON_ENUMERATION · All error codes

What went wrong

An operator (or method) is applied to the ENUMERATION TYPE itself, for example '$CardSuit' or 'CardSuit > CardSuit.Hearts'. The type is not a value: operators apply to the enumeration's values, such as 'CardSuit.Hearts' or a variable that holds one.

Worked examples

Compilable examples demonstrating this error and the correct fix:

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

What works on a value

An enumeration's values are ordered (in declaration order), so the comparison operators (<, <=, >, >=, ==, <>, <=>) work on them, as do '$' (the name of the value), '?', '#?' and '#<' / '#>' (the first and last value). Iterate the type itself with 'for suit in CardSuit' or 'cat CardSuit'.

How to fix it

  1. Apply the operator to a value: CardSuit.Hearts, or a variable holding one
  2. To visit every value, iterate the type: for suit in CardSuit

Common causes

  • Writing the type name where a value was meant
  • Expecting '$' on the type to list its values

See also

  • E07630: operator applied to the enumeration type (this error)
  • E07620: operator not defined for type

Examples

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

Fails with E07630: operator cannot be used on an Enumeration in this way

#!ek9
defines module error.e07630.bad

  defines type

    CardSuit
      Hearts
      Diamonds
      Clubs
      Spades

  defines function

    describeSuits()
      stdout <- Stdout()

      //CardSuit is the type; '$' applies to one of its values
      stdout.println($CardSuit)

Fixed: Apply the operator to a value of the enumeration

#!ek9
defines module error.e07630.good

  defines type

    CardSuit
      Hearts
      Diamonds
      Clubs
      Spades

  defines program

    DescribeSuits()
      stdout <- Stdout()

      //Every value of the enumeration, in order
      for suit in CardSuit
        stdout.println($suit)

      //Operators apply to values, and the values are ordered
      hearts <- CardSuit.Hearts
      stdout.println($hearts)
      stdout.println($(hearts < CardSuit.Spades))

Questions and answers

These Q&A entries show E07630 and its fix:

Found a problem?

If EK9 reported E07630 for a program you think is valid, or did not report it where it should have, use the compiler form with the smallest program that shows it. If this page is wrong or unclear, use the documentation form. Both are at github.com/ek9lang/ek9-issues.