I get E05120 saying override is required on my operator ?. How do I fix it?

← Override Mechanics · Ref: Q1068

Every EK9 class inherits operator ? from its base type. When you define your own operator ?, you are OVERRIDING the inherited one. The compiler requires the 'override' keyword to make this explicit.

BEFORE (E05120 error):

  operator ? as pure
    <- rtn as Boolean: name? and age?

AFTER (fixed):

  override operator ? as pure
    <- rtn as Boolean: name? and age?

The same applies when you use 'default operator' alongside a manual operator ?. Write the manual one BEFORE 'default operator' (which must be the last item in the class body - anything after it is the syntax error E01086); 'default operator' then generates only what you did not write:

  override operator ? as pure
    <- rtn as Boolean: name? and age?
  default operator

The 'override' keyword is required on an operator that already exists in a parent type - and only there. For operator ?, it is always required because every type inherits it.

Which operators need 'override':

  override operator ?    isSet - always (every type inherits it)
  override operator $    string - only if a PARENT type declares $. Written beside 'default operator' with no parent $, it takes no 'override' (E05110 'has no parent version').

Sameness operators are different: '==' or '<=>' is declared once per type hierarchy, and a subtype inherits it and can NOT redefine it, with or without 'override' (E07182).

See Q574 for override mechanics. See Q877 for the ? operator. See Q1055 for default vs manual operators.

Example

defines module qa.overridemechanics.fixe05120

  defines class

    Person
      personName as String: String()
      age as Integer: 0

      default private Person()

      Person()
        ->
          personName as String
          age as Integer
        this.personName :=: personName
        this.age :=: age

      //CORRECT: override is required because ? is inherited
      override operator ? as pure
        <- rtn as Boolean: personName? and age?

      operator $ as pure
        <- rtn as String: `${personName} (${age})`

  defines program

    OverrideDemo()
      stdout <- Stdout()

      person <- Person("Alice", 30)
      stdout.println(`Is set: ${person?}`)
      stdout.println(`${person}`)

Common mistakes

E05120 — Add 'override' before 'operator ?'. Every class inherits operator ? from its base type, so your definition overrides it.

Incorrect:

      operator ? as pure
        <- rtn as Boolean: personName? and age?

Correct:

      override operator ? as pure
        <- rtn as Boolean: personName? and age?
Other ways to ask this
  • The compiler says override required on operator ? in my class
  • Fix E05120: override required on isSet operator
  • Why do I need override on operator ? when I defined it myself?

Coming from another language?

Java: @Override annotation (optional but recommended). EK9: override keyword (mandatory). The compiler enforces it — no optional about it.

Keywords: operator, E05120, class, isSet, fix, override, required