E06140: ambiguous match

Category: E06xxx Type hierarchy checks · Reported in phase: FULL_RESOLUTION · Classification: METHOD_AMBIGUOUS · All error codes

What went wrong

Multiple overloaded methods match your call with EQUAL cost. The compiler cannot determine which method you intended to call because two or more overloads have the same matching score.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 255 (How does method resolution work with overloading?)

How to fix it

  1. Cast/declare argument to specific type to disambiguate
  2. Make method signatures more distinct (add/change parameters)
  3. Reduce overloading - use named methods (methodForC1, methodForC2)

Common causes

  • Type hierarchy combined with overloads
  • Two methods differ only by auto-promotable parameter
  • Trait/class inheritance creates multiple matching paths

See also

  • E06140: multiple methods match EQUALLY (this error)
  • E50060: NO method matches at all (METHOD_NOT_RESOLVED)

https://ek9.io/methods.html https://ek9.io/forAI.json#$.methodResolution

Examples

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

Fails with E06140: ambiguous match

#!ek9
defines module error.e06140.bad

  defines trait

    Valued
      amount() as abstract
        <- rtn as Integer?

  defines class

    A as open
      default A() as pure

    B extends A as open
      default B() as pure

    C extends B with trait of Valued
      default C() as pure

      override amount()
        <- rtn <- 42

    Checker
      check()
        -> candidate as A
        <- rtn as Boolean: candidate?

      check()
        -> candidate as Valued
        <- rtn as Boolean: candidate?

  defines program

    Ambiguous()
      stdout <- Stdout()
      checker <- Checker()
      //C reaches A through B (0.05 + 0.05) and Valued directly (0.10): equal cost
      stdout.println($checker.check(C()))

Fixed: Make one candidate a closer match - here B, one step from C - so the costs differ

#!ek9
defines module error.e06140.good

  defines trait

    Valued
      amount() as abstract
        <- rtn as Integer?

  defines class

    A as open
      default A() as pure

    B extends A as open
      default B() as pure

    C extends B with trait of Valued
      default C() as pure

      override amount()
        <- rtn <- 42

    Checker
      check()
        -> candidate as B
        <- rtn as Boolean: candidate?

      check()
        -> candidate as Valued
        <- rtn as Boolean: candidate?

  defines program

    Unambiguous()
      stdout <- Stdout()
      checker <- Checker()
      stdout.println($checker.check(C()))

Questions and answers

These Q&A entries show E06140 and its fix: