E05220: dispatcher handler return type is not covariant with dispatcher entry point return type
What went wrong
A dispatcher handler method has a return type that is incompatible with the dispatcher entry point's declared return type. The caller expects the entry point's return type, but depending on which handler is selected at runtime, they could receive an incompatible type.
Worked examples
Compilable examples demonstrating this error and the correct fix:
- ek9 -q 615 (How does EK9 check dispatcher return type compatibility?)
- ek9 -q 614 (What happens if a dispatcher handler's parameter type is ...)
- ek9 -q 619 (What happens with two-parameter dispatchers and type vali...)
How to fix it
- Change the handler's return type to match the dispatcher entry point's return type
- Or use a subtype (covariant) of the dispatcher's return type
- Ensure void/non-void consistency: if the dispatcher returns a value, all handlers must too
Common causes
- Handler returns a different type than the dispatcher entry point
- Mixing void and non-void methods in the same dispatcher group
- Copy-paste from a different dispatcher with a different return type
See also
Examples
Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.
Fails with E05220: dispatcher handler return type is not covariant with dispatcher entry point return type
#!ek9 defines module error.e05220.bad defines class Formatter format() as dispatcher -> input as Any <- rtn as String: "unknown" //An Integer is not a String: a handler returns what the dispatcher returns format() -> input as Integer <- rtn as Integer: input
Fixed: Return the dispatcher's type (or a sub-type of it) from every handler
#!ek9 defines module error.e05220.good defines class Formatter format() as dispatcher -> input as Any <- rtn as String: "unknown" format() -> input as Integer <- rtn as String: $input
Questions and answers
These Q&A entries show E05220 and its fix: