What are the restrictions on this and super in EK9?
← Code Quality · Ref: Q743
EK9 enforces specific rules about how 'this' and 'super' can be used.
SUPER FOR ANY NOT REQUIRED (E05040)
All EK9 classes implicitly extend 'Any'. Calling super() in a constructor when there is no explicit parent class is unnecessary and rejected. Only call super() when the class explicitly extends another class.
INAPPROPRIATE USE OF THIS OR SUPER (E05090)
You cannot directly assign to 'this' or 'super' using ':=', ':=?', or ':'. These would attempt to replace the object pointer itself, which is not meaningful. Use ':=:' (copy) or ':~:' (merge) instead to copy state from another object.
ALLOWED OPERATIONS
- this :=: other (copy state from other)
- this :~: other (merge state from other)
- $this (string conversion)
- this? (isSet check)
See Q580 for this delegation. See Q581 for super delegation.
Example
defines module qa.codequality.thissuperrestrictions defines class Base as open label <- String() Base() -> label as String this.label: label describe() <- rtn as String: label default operator Child extends Base role <- String() Child() -> label as String role as String //super() IS valid here because Child explicitly extends Base super(label) this.role: role override describe() <- rtn as String: `${super.describe()} (${role})` default operator defines program ThisSuperDemo() stdout <- Stdout() child <- Child("Alice", "Engineer") stdout.println(child.describe()) base <- Base("Bob") stdout.println(base.describe())
Common mistakes
E05040 — Base has no explicit parent, so calling super() is unnecessary. All classes implicitly extend Any. Remove the super() call. See ek9 -h E05040 for details.
Incorrect:
Base()
-> label as String
super()
this.label: label
Correct:
Base()
-> label as String
this.label: label
E50060 — EK9 has no toString() method. Use the describe() method or string interpolation. See ek9 -h E50060 for details.
Incorrect:
stdout.println(child.toString())
Correct:
stdout.println(child.describe())
E50060 — The method is describe(), not getLabel(). EK9 does not use Java-style getter naming. See ek9 -h E50060 for details.
Incorrect:
stdout.println(base.getLabel())
Correct:
stdout.println(base.describe())
E05090 — Cannot assign directly to 'this'. Use 'this.fieldName' to access a specific field, or use :=:, :~:, += etc. for aggregate operations. See ek9 -h E05090 for details.
Incorrect:
this: label
Correct:
this.label: label
Other ways to ask this
- What is E05040 super for Any not required in EK9?
- What is E05090 inappropriate use of this or super in EK9?
- Why does EK9 reject super() in my constructor?
Coming from another language?
Java: super() implicitly inserted if omitted. Python: super().__init__() must be called explicitly. Rust: no super concept. Go: no inheritance. Kotlin: super() implicitly called. EK9: super() only valid with explicit parent, this/super cannot be reassigned.
Keywords: restriction, merge, quality, constructor, super, E05040, copy, E05090, assignment, this