E04040: type must be a function or delegate

Category: E04xxx Reference checks · Reported in phase: FULL_RESOLUTION · Classification: TYPE_MUST_BE_FUNCTION · All error codes

What went wrong

A function or function delegate is required, but you provided something that is not callable (like a class instance or record).

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 842 (Why does streaming non-function values through call fail ...)

Dynamic function syntax

When using 'is Function of (T, R)', variable names come from the generic type:

  • t = input parameter (type T)
  • r = return value (type R)

The 'function' keyword is REQUIRED to distinguish from dynamic class syntax.

How to fix it

  • Pass a function name (not a function call result)
  • Create a dynamic function matching the expected signature
  • Ensure the variable holds a function delegate, not a class instance

Common causes

  • Passing class instance instead of function
  • Using () to call function instead of passing it as delegate
  • Wrong type variable (not a Function of (...))

See also

  • E07480: not a function delegate

Examples

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

Fails with E04040: type must be a function or delegate

#!ek9
defines module error.e04040.bad

  defines function

    printNumbers()
      stdout <- Stdout()

      //'call' calls each item it is given, so each item must be a function
      cat [1, 2] | call > stdout

Fixed: Stream functions into 'call' - or 'map with' a function to transform plain values

#!ek9
defines module error.e04040.good

  defines function

    nameSource() as abstract
      <- rtn as String?

    firstName() is nameSource
      <- rtn <- "Steve"

    lastName() is nameSource
      <- rtn <- "Limb"

    doubled()
      -> number as Integer
      <- rtn as Integer: number * 2

    printNames()
      stdout <- Stdout()
      cat [firstName, lastName] | call > stdout

    printNumbers()
      stdout <- Stdout()
      cat [1, 2] | map with doubled > stdout

Questions and answers

These Q&A entries show E04040 and its fix: