E07650: use ~ for the not operator

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

What went wrong

A type declares 'operator not'. The negation operator a type declares is '~'. In an expression both 'not ready' and '~ready' negate (one operator, '~'), but the operator is DECLARED only by its symbol: 'operator ~'. ('!ready' is not EK9 at all: it is a syntax error.)

How to fix it

  1. Rename the declaration to 'operator ~', keeping its body and return type
  2. Callers may go on writing 'not x' or '~x'; both reach the '~' you declared

The [EXAMPLE] section below shows 'operator not' declared, and a Toggle that declares 'operator ~' instead.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 1321 (Why can't I declare 'operator not' on a type in EK9?)

Rationale

One operator has one declared name. Word 'not' is how negation reads in an expression, but the operator itself is '~' (mathematical complement), so a type declares '~' and every spelling of negation reaches it. The '!' character is reserved for other purposes in EK9 grammar (notably the injection marker for dependency-injection fields), so it is no negation at all.

Common causes

  • Naming the operator after the word used in expressions ('not x')
  • Habit from Python, where 'not' is the operator's name
  • Copy-paste from a non-EK9 source

See also

  • E07640: use <> for the not equal operator

Examples

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

Fails with E07650: use ~ for the not operator

#!ek9
defines module error.e07650.bad

  defines class

    Toggle
      on <- false

      //Negation is the '~' operator
      default operator ?

      operator not as pure
        <- rtn as Toggle: this

Fixed: Declare negation as '~'

#!ek9
defines module error.e07650.good

  defines class

    Toggle
      on as Boolean?

      default private Toggle() as pure

      Toggle() as pure
        -> initial as Boolean
        this.on :=? initial

      default operator ?

      operator ~ as pure
        <- rtn as Toggle: Toggle(not on)

Questions and answers

These Q&A entries show E07650 and its fix: