What happens if a function extension has a signature mismatch?

← Function Extension · Ref: Q592

When a function extends an abstract function, the signatures must match. If the parameter types, parameter count, or return type differ, the compiler reports E05140.

THE ERROR: E05140

E05140 fires when a function declares 'is ParentFunction' but the signatures do not match. Common mismatches: different parameter types, wrong parameter count, incompatible return type.

SIGNATURE MATCHING RULES

Parameter count must be identical. Parameter types must be identical (or contravariant). Return type must be identical (or covariant). Parameter names can differ (names are local).

COMMON MISTAKES

Wrong parameter type (Integer vs String). Extra or missing parameters. Return type mismatch.

CORRECT APPROACH

Check the abstract function signature and match it exactly in the implementation. Parameter names can be different, but types must match.

See Q588 for function extension basics. See Q589 for multiple implementations. See Q594 for extension rules.

Example

defines module qa.function.signaturematch

  defines function

    //Abstract with specific signature
    StringProcessor as pure abstract
      -> input as String
      <- output as String?

    //Correct: exact signature match (parameter names differ, types match)
    TrimProcessor is StringProcessor as pure
      -> input as String
      <- output as String: input.trim()

    //Correct: another match
    UpperProcessor is StringProcessor as pure
      -> input as String
      <- output as String: input.upperCase()

    //Different abstract with two parameters
    Combiner as pure abstract
      ->
        first as String
        second as String
      <- combined as String?

    //Correct: matches two-parameter signature
    SpaceCombiner is Combiner as pure
      ->
        first as String
        second as String
      <- combined as String: `${first} ${second}`

    DashCombiner is Combiner as pure
      ->
        first as String
        second as String
      <- combined as String: `${first}-${second}`

  defines program

    FunctionSignatureMatchDemo()
      stdout <- Stdout()

      //Single-parameter functions
      processors <- List() of StringProcessor
      processors += TrimProcessor
      processors += UpperProcessor

      for processor in processors
        stdout.println(processor("  hello  "))

      //Two-parameter functions
      combiners <- List() of Combiner
      combiners += SpaceCombiner
      combiners += DashCombiner

      for combiner in combiners
        stdout.println(combiner("hello", "world"))

Common mistakes

E50001 — A pure function cannot call non-pure methods. Pure functions must not produce any side effects. See ek9 -h E50001 for details.

Incorrect:

SpaceCombiner is Combiner as pure
      ->
        first as String
        second as String
      <- combined as String: `${first} ${second}`
      stdout.println(combined)

Correct:

SpaceCombiner is Combiner as pure
      ->
        first as String
        second as String
      <- combined as String: `${first} ${second}`
Other ways to ask this
  • What is E05140 function signature does not match super?
  • Why does my function extension fail with a signature error?
  • How do I fix a function signature mismatch?

Coming from another language?

Java: functional interface method signature must match exactly. Kotlin: functional type signatures are structural. Rust: closure type must match Fn trait signature. EK9: E05140 enforces exact signature match for function extension.

Keywords: function, match, return, E05140, signature, extend, type, mismatch, count, abstract, parameter