Why can't I declare 'operator !=' on my EK9 type?

← Operators and Expressions · Ref: Q1320

EK9's not-equal operator is '<>' (mathematical notation), not the C-family '!='. When you declare an operator on a type you must name it '<>'. Declaring 'operator != as pure' triggers E07640 (BAD_NOT_EQUAL_OPERATOR) because '!=' is not a valid EK9 operator symbol for a declaration.

Replace 'operator !=' with 'operator <>'. The operator must be pure, take exactly one argument of the same type, and return Boolean. EK9 chose '<>' because it is the mathematical not-equal form (the same as Pascal, SQL and BASIC) and composes naturally with '<' and '>'.

See Q238 for the complete fixed operator set. See Q239 for comparison and ordering operators.

Example

defines module qa.operators.notequal

  defines class

    Score
      points <- Integer()

      Score() as pure
        -> points as Integer
        this.points :=: points

      operator == as pure
        -> other as Score
        <- rtn as Boolean: points == other.points

      //THE FIX: EK9's not-equal operator is '<>', NOT '!='.
      //Declaring 'operator !=' would trigger E07640.
      operator <> as pure
        -> other as Score
        <- rtn as Boolean: points <> other.points

      operator $ as pure
        <- rtn as String: $points

      override operator ? as pure
        <- rtn as Boolean: points?

  defines program

    NotEqualOperatorDemo()
      stdout <- Stdout()

      a <- Score(10)
      b <- Score(20)

      stdout.println(`Equal: ${a == b}`)
      stdout.println(`Not equal: ${a <> b}`)

Common mistakes

E07640 — EK9 uses the mathematical not-equal symbol '<>', not the C-family '!='. Declaring 'operator !=' is not a valid EK9 operator name and triggers E07640. Rename the declaration to 'operator <>' (pure, one argument, returns Boolean). See ek9 -h E07640 for details.

Incorrect:

operator != as pure
  -> other as Score
  <- rtn as Boolean: points <> other.points

Correct:

operator <> as pure
        -> other as Score
        <- rtn as Boolean: points <> other.points
Other ways to ask this
  • What triggers E07640 BAD_NOT_EQUAL_OPERATOR?
  • How do I implement not-equal on a custom EK9 class?
  • Why does EK9 reject '!=' as an operator name?

Coming from another language?

Java/C++/Python/Rust/JavaScript: not-equal is '!=' and (where overloading exists) you implement it under that symbol or as a negated equals. EK9: the only not-equal operator symbol is '<>', and the compiler rejects a '!=' declaration at compile time (E07640, phase EXPLICIT_TYPE_SYMBOL_DEFINITION) rather than silently accepting an alternate spelling.

Keywords: comparison, declaration, not, E07640, !=, inequality, <>, operator, equal, neq