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
E50060 — TraitName.methodName() must name a method the trait really declares. LeftInfo has getInfo(), not getDetails(), so the call cannot be resolved and the compiler reports E50060. EK9 has no casting, so there is no ((LeftInfo)this) form to reach it. See ek9 -h E50060 for details.
Incorrect:
<- rtn as String: LeftInfo.getDetails()
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: trait, specific, explicit, TraitName, call, disambiguate