E07640: use <> for the not equal operator

Category: E07xxx Full resolution · Reported in phase: EXPLICIT_TYPE_SYMBOL_DEFINITION · Classification: BAD_NOT_EQUAL_OPERATOR · All error codes

What went wrong

A type declares 'operator !='. EK9's not-equal operator is '<>', and a type never writes it: '<>' is generated from the type's sameness - its '==' (equatable only) or its '<=>' (ordered) - so the two ways of saying "equal" cannot disagree (E07181). In an EXPRESSION 'a != b' is accepted and means 'a <> b'; it is only declaring an operator named '!=' that is refused.

How to fix it

  1. Remove 'operator !='
  2. Give the type its sameness instead: 'default operator ==' (or '<=>') - or write '==' (or '<=>') yourself - and '<>' comes with it
  3. Compare with '<>' (or '!=') at the call site as before

The [EXAMPLE] section below shows 'operator !=' declared, and a Point whose '<>' is generated from its '==' and used in a comparison.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 1320 (Why can't I declare 'operator !=' on my EK9 type?)

Rationale

A type defines sameness once, and every comparison is generated from it. An operator called '!=' would be a second, hand-written not-equal beside the generated '<>', free to disagree with '=='. '<>' is the operator's one name - the form used in Pascal, SQL and BASIC - and composes with '<' and '>' as a family.

Common causes

  • Habit from C++ or Kotlin, where 'operator!=' is declared by hand
  • Porting a type that implemented equals() and a separate not-equals
  • Copy-paste from a non-EK9 source

See also

  • E07650: use ~ for the not operator
  • E07181: a comparison written by hand that is generated from sameness

Examples

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

Fails with E07640: use <> for the not equal operator

#!ek9
defines module error.e07640.bad

  defines class

    Point

      //'<>' is EK9's not-equal
      operator != as pure
        -> other as Point
        <- rtn as Boolean: true

Fixed: Use '<>', generated from the type's sameness

#!ek9
defines module error.e07640.good

  defines class

    Point
      x <- 0
      y <- 0

      default private Point()

      Point()
        ->
          across as Integer
          down as Integer

        this.x: across
        this.y: down

      //Equatable sameness: '<>' is generated from '=='
      default operator ==

      default operator ?

      default operator #?

  defines program

    ComparePoints()
      stdout <- Stdout()
      first <- Point(1, 2)
      second <- Point(2, 1)
      if first <> second
        stdout.println("different points")

Questions and answers

These Q&A entries show E07640 and its fix:

Found a problem?

If EK9 reported E07640 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.