E02030: duplicate/ambiguous methods/operations

Category: E02xxx Symbol definition · Reported in phase: EXPLICIT_TYPE_SYMBOL_DEFINITION · Classification: METHOD_DUPLICATED · All error codes

What went wrong

Two or more methods have the same signature (name + parameter types). Method overloading requires different parameter types, not just different names.

Two spellings of ONE operator are the same method: unary '-' and '~' are both negation. A type may declare it once, with one spelling, and a sub-type spells an operator it inherits the way its parent does ('override operator ~' below a '~'), or it declares the same operator again without overriding it. Two parents (two traits, or a super and a trait) must spell it the same way too.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 1069 (Can I use default operator and also define my own operato...)
  • ek9 -q 949 (What are the rules for using 'default operator' in EK9?)

How to fix it

  • Remove the duplicate method definition
  • Change parameter types to create a distinct overload
  • Rename one method if different behavior is needed
  • For unary '-' and '~': keep one of them; in a sub-type, use the parent's spelling with 'override'; across two parents, spell it one way in both

Common causes

  • Copy-paste creating duplicate method definitions
  • Forgetting a method with same signature already exists
  • Overloaded methods that accidentally have identical signatures
  • Declaring both unary '-' and '~' (one operator, negation), in one type or across a type and its super or trait, or across two of its parents

See also

  • E02030: Method signature duplication (DEFINITION time)
  • E06140: Ambiguous method CALL (multiple methods match equally)
  • E05120: Method overriding issues in INHERITANCE

Examples

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

Fails with E02030: duplicate/ambiguous methods/operations

#!ek9
defines module error.e02030.bad

  defines class

    Calculator

      add()
        ->
          first as Integer
          second as Integer
        <- rtn as Integer: first + second

      //Same name, same parameter types: a duplicate, whatever the body does
      add()
        ->
          first as Integer
          second as Integer
        <- rtn as Integer: first + second + 1

Fixed: Give the second method a name that says what it does differently

#!ek9
defines module error.e02030.good

  defines class

    Calculator

      addWithBonus()
        ->
          first as Integer
          second as Integer
        <- rtn as Integer: first + second + 1

      add()
        ->
          first as Integer
          second as Integer
        <- rtn as Integer: first + second

Fixed: Or overload it: the same name with different parameter types is a different method

#!ek9
defines module error.e02030.good.overload

  defines class

    Calculator

      add()
        ->
          first as Integer
          second as Integer
        <- rtn as Integer: first + second

      add()
        ->
          first as Float
          second as Float
        <- rtn as Float: first + second

Questions and answers

These Q&A entries show E02030 and its fix:

Found a problem?

If EK9 reported E02030 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.