How do I avoid dispatcher handler ambiguity with multiple traits?
← Dispatcher Validation · Ref: Q858
When a type implements two traits and handlers exist for both traits at equal cost, the dispatcher cannot choose. Add a specific handler for the ambiguous type.
THIS EXAMPLE
Duck implements Flyable. The processor has a handler for Dog (concrete). Replacing the Dog handler with a Flyable handler would create ambiguity for Duck if Swimmable were also handled.
See Q616 for dispatcher ambiguity. See Q621 for dispatch rules.
Example
defines module qa.dispatcher.no.ambiguity defines trait Flyable fly() Stdout().println("Flying") Swimmable swim() Stdout().println("Swimming") defines class Animal as abstract speak() as abstract default operator ? Dog is Animal override speak() Stdout().println("Woof") Duck is Animal with trait of Flyable, Swimmable override speak() Stdout().println("Quack") override operator ? as pure <- rtn as Boolean: true AnimalProcessor process() as dispatcher -> animal as Animal animal.speak() process() -> animal as Dog animal.speak() default operator ? defines program DispatcherDemo() processor <- AnimalProcessor() dog as Animal: Dog() if processor? and dog? processor.process(dog)
Other ways to ask this
- What triggers E05230 DISPATCHER_HANDLER_AMBIGUITY?
- Why is Duck ambiguous when it implements two traits?
- How do I resolve dispatcher ambiguity in EK9?
Coming from another language?
Java: method overloading is compile-time. C#: dynamic dispatch at runtime. Kotlin: sealed when is exhaustive. EK9: compile-time ambiguity detection for all reachable runtime types.
Keywords: E05230, dispatcher, trait, ambiguity, multiple, handler