E07810: Dispatch only supported in classes

Category: E07xxx Full resolution · Reported in phase: SYMBOL_DEFINITION · Classification: DISPATCH_ONLY_SUPPORTED_IN_CLASSES · All error codes

What went wrong

You're using 'dispatcher' keyword outside of a class context. Dispatch (multiple dispatch pattern) is only supported in classes.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 106 (What are traits and how do I use them in EK9?)

How to fix it

  1. Move dispatcher method to a class
  2. Dispatch pattern requires class instance context

Common causes

  • Dispatcher in function
  • Dispatcher in trait

See also

  • E07810: dispatcher outside class (this error)
  • E07820: multiple dispatchers in class

Examples

Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.

Fails with E07810: Dispatch only supported in classes

#!ek9
defines module error.e07810.bad

  defines trait

    Describer

      //Dispatch chooses a method on a class's actual argument types
      describe() as dispatcher
        -> shape as Shape
        <- rtn as String: shape.kind()

  defines class
    Shape as open
      kind()
        <- rtn as String: "shape"

Fixed: Put the dispatcher on a class

#!ek9
defines module error.e07810.good

  defines class

    Shape as open
      kind()
        <- rtn as String: "shape"

    Circle extends Shape
      override kind()
        <- rtn as String: "circle"

    Describer
      describe()
        -> circle as Circle
        <- rtn as String: "a round " + circle.kind()

      describe() as dispatcher
        -> shape as Shape
        <- rtn as String: "some " + shape.kind()

Questions and answers

These Q&A entries show E07810 and its fix:

Found a problem?

If EK9 reported E07810 for a program you think is valid, or did not report it where it should have, use the compiler form with the smallest program that shows it. If this page is wrong or unclear, use the documentation form. Both are at github.com/ek9lang/ek9-issues.