How does EK9 validate argument counts when calling generic functions?

← Generics · Ref: Q656

EK9 validates that the number of arguments passed to a function, method or constructor matches the declared parameter count. The error code depends on what is being called: a plain function gives E06270, a generic function gives E06020, and a method or constructor gives E50060.

EXACT MATCH REQUIRED

A function declared with two parameters:

  combine()
    ->
      a as String
      b as String
    <- rtn as String: a + b

Must be called with exactly two arguments:

  combine("hello", "world")  // correct

TOO MANY OR TOO FEW (FUNCTION, E06270)

Passing the wrong number of arguments to a non-generic function is a parameter mismatch:
Calling combine("a", "b", "c") or combine("a") is E06270: the supplied arguments are not appropriate (parameter mismatch).

GENERIC FUNCTION CALLS (E06020)

A generic function whose type is inferred from the call is checked the same way, with its own code:
If transform takes exactly one argument (source as S), then calling transform(x, y) is E06020: type/function is generic, but incorrect number of parameters supplied.

METHODS AND CONSTRUCTORS (E50060)

Calling a method or constructor with the wrong number of arguments reports that the method could not be resolved:
If add takes two arguments, calling calc.add(1) is E50060: parameter mismatch, method/function not resolved.

NO OPTIONAL PARAMETERS

EK9 has no default parameter values (by design). Every declared parameter must receive an argument.

See Q597 for no default parameters design. See Q642 for constructor inference. See Q654 for type parameter count.

Example

defines module qa.genericsdeep.argumentcount

  defines function

    <?-
      Single parameter function. Requires exactly 1 argument.
    -?>
    square() as pure
      -> number as Integer
      <- rtn as Integer: number * number

    <?-
      Two parameter function. Requires exactly 2 arguments.
    -?>
    multiply() as pure
      ->
        factor1 as Integer
        factor2 as Integer
      <- rtn as Integer: factor1 * factor2

    <?-
      Generic single parameter function.
    -?>
    echo() of type T as open
      -> item as T
      <- rtn as T: T(item)

  defines program

    ArgumentCountDemo()
      stdout <- Stdout()

      // === SINGLE ARG: exactly 1 ===

      sq <- square(7)
      stdout.println(`Square of 7: ${sq}`)

      // === TWO ARGS: exactly 2 ===

      product <- multiply(factor1: 6, factor2: 7)
      stdout.println(`Product: ${product}`)

      // === GENERIC SINGLE ARG ===

      echoed <- echo(42)
      stdout.println(`Echoed: ${echoed}`)

      strEchoed <- echo("hello")
      stdout.println(`Str echoed: ${strEchoed}`)

Common mistakes

E06270 — square() declares exactly one parameter, so passing two arguments triggers E06270 (too many arguments). See ek9 -h E06270 for details.

Incorrect:

sq <- square(7, 3)

Correct:

sq <- square(7)

E06270 — multiply() declares two parameters (factor1 and factor2), so passing only one argument triggers E06270 (too few arguments). See ek9 -h E06270 for details.

Incorrect:

product <- multiply(factor1: 6)

Correct:

product <- multiply(factor1: 6, factor2: 7)
Other ways to ask this
  • What error do I get for too many or too few arguments in a function call?
  • How does EK9 check function call argument counts?
  • What happens if I pass wrong number of arguments to a generic function?

Coming from another language?

Java: exact argument count required (no defaults for methods). C++: default parameters allow fewer args. Rust: exact count required (no defaults). Go: exact count required (no defaults or varargs for regular funcs). Kotlin: default parameters allow fewer. Python: default params and *args. EK9: exact count always, no defaults by design (E06270 for functions, E06020 for generic functions, E50060 for methods and constructors).

Keywords: validation, E06020, E06270, function, argument, count, E50060, generic, too few, too many, type-parameter