E02080: use of a delegate and methods of same name

Category: E02xxx Symbol definition · Reported in phase: FULL_RESOLUTION · Classification: DELEGATE_AND_METHOD_NAMES_CLASH · All error codes

What went wrong

A function delegate field has the same name as a method in the class. This creates ambiguity: does 'name()' call the method or invoke the delegate?

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 57 (How can I change method behaviour on a class without subc...)

How to fix it

  • Rename the delegate field (add 'Delegate' suffix)
  • Or rename the method to be distinct

Rationale

When you write 'obj.processor("input")', EK9 cannot determine:

  • Should it call the method 'processor()'?
  • Should it invoke the delegate stored in 'processor' field?

Common causes

  • Using same naming convention for delegates and methods
  • Refactoring that introduced a naming collision

See also

  • E02030: duplicate/ambiguous methods
  • E06140: ambiguous match

Examples

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

Fails with E02080: use of a delegate and methods of same name

#!ek9
defines module error.e02080.bad

  defines class

    Counter

      current()
        <- rtn as Integer?

        //A delegate named like the method below: which one does next() call?
        next <- () is NumberSource as function (rtn: 22)
        rtn: next()

      next()
        <- rtn as Integer: 1

  defines function

    NumberSource() as abstract
      <- rtn as Integer?

Fixed: Give the delegate a name no method has, such as one ending in Source or Handler

#!ek9
defines module error.e02080.good

  defines class

    Counter

      current()
        <- rtn as Integer?
        nextSource <- () is NumberSource as function (rtn: 22)
        rtn: nextSource() + next()

      next()
        <- rtn as Integer: 1

  defines function

    NumberSource() as abstract
      <- rtn as Integer?

Questions and answers

These Q&A entries show E02080 and its fix:

Found a problem?

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