Call a specific trait's method implementation using TraitName.method() syntax.
← Sealed Types and Traits · Ref: Q1132
Call a specific trait's implementation by name:
getLeftInfo() as pure <- rtn as String: LeftInfo.getInfo() getRightInfo() as pure <- rtn as String: RightInfo.getInfo()
TraitName.methodName() bypasses override resolution and goes directly to the specified trait.
See Q1131 for diamond resolution. See Q1129 for basic delegation.
Example
defines module qa.sealedtraits.traitexplicitcall defines trait LeftInfo getInfo() as pure <- rtn as String: "left-info" RightInfo getInfo() as pure <- rtn as String: "right-info" defines class Combined with trait of LeftInfo, RightInfo //Must override the conflicting method override getInfo() as pure <- rtn as String: "combined" //Explicit calls to each trait's version getFromLeft() as pure <- rtn as String: LeftInfo.getInfo() getFromRight() as pure <- rtn as String: RightInfo.getInfo() default operator ? defines program TraitExplicitCallDemo() stdout <- Stdout() combined <- Combined() stdout.println(`Default: ${combined.getInfo()}`) stdout.println(`Left: ${combined.getFromLeft()}`) stdout.println(`Right: ${combined.getFromRight()}`)
Common mistakes
E01010 — EK9 has no casting. Use TraitName.methodName() to call a specific trait's implementation directly.
Incorrect:
<- rtn as String: ((LeftInfo)this).getInfo()
Correct:
<- rtn as String: LeftInfo.getInfo()
Other ways to ask this
- I need to access a particular trait's version of a method in a class with multiple traits
- In Java I'd use InterfaceName.super.method(). Write the EK9 explicit trait call
- Given a class with two traits providing getInfo(), call each trait's version explicitly
- Disambiguate trait method calls using the TraitName.methodName() syntax
Coming from another language?
Java: InterfaceName.super.method(). C#: ((IInterface)this).Method(). EK9: TraitName.method() — direct, no casting needed.
Keywords: TraitName, explicit, specific, trait, disambiguate, call