When can I use super in EK9?

← Code Quality · Ref: Q822

EK9 restricts the use of 'super' to specific contexts within subclass methods. Using super as a standalone expression, assigning it to a variable, or passing it as an argument is not allowed.

VALID USES OF SUPER

Super is valid only for calling parent methods:

  super.methodName()         // call parent implementation

INVALID USES OF SUPER

  theSuper <- super          // ERROR: can't assign super to variable
  theSuper := super          // ERROR: can't reassign to super
  this.callMethod(super)     // ERROR: can't pass super as argument

WHY THIS RESTRICTION

Super is not an object reference — it is a dispatch mechanism. Allowing super as a value would create aliasing problems and break the inheritance contract. EK9 enforces that super is only used for method dispatch.

See Q96 for inheritance. See Q97 for class vs record.

Example

defines module qa.quality.super.misuse

  defines class

    BaseWidget as open
      getName() as pure
        <- rtn as String: "BaseWidget"
      default operator ?

    DerivedWidget extends BaseWidget
      override getName() as pure
        <- rtn as String: "DerivedWidget"

      displayParentName()
        stdout <- Stdout()
        // === CORRECT: super for method dispatch ===
        name <- super.getName()
        stdout.println(`Parent: ${name}`)

  defines program

    SuperMisuseDemo()
      widget <- DerivedWidget()
      widget.displayParentName()

Common mistakes

E05080 — Super cannot be used as a standalone expression or converted to a string. It is only valid for dispatching method calls to the parent class. See ek9 -h E05080 for details.

Incorrect:

        name <- super

Correct:

        name <- super.getName()
Other ways to ask this
  • What is E05080 INAPPROPRIATE_USE_OF_SUPER?
  • Why does EK9 reject my use of super?
  • Where is super valid in EK9?

Coming from another language?

Java: super can be used for method calls and constructor chaining but not as a standalone value. Python: super() returns a proxy object. Rust: no super — use trait default methods. Go: embedded struct accessed by name. EK9: super restricted to method dispatch only.

Keywords: class, inheritance, E05080, dispatch, super, parent, method