What happens when you pass the wrong kind of construct as a parameter?

← Advanced Type System · Ref: Q951

Parameter genus (fundamental KIND) must match — can't pass a class where a function is expected.

EK9 validates not just types but the fundamental kind (genus) of each argument. A class, function, record, trait, and service are different genera. Passing one where another is expected triggers E50090.

GENUS CATEGORIES

  CLASS — instantiated objects with methods
  FUNCTION — callable units
  RECORD — data-only aggregates
  TRAIT — interface contracts
  SERVICE — HTTP endpoints

EXAMPLE

If a function expects a function parameter, passing a class instance fails:

  doWork()
    -> action as Assessor of String
    ...
  doWork(MyClass())    // E50090 if MyClass is not an Assessor

See Q254 for Any type. See Q258 for type coercion. See Q255 for method resolution.

Example

defines module qa.types.parametergenus

  defines function

    <?-
      Function that accepts a predicate.
    -?>
    countMatching()
      ->
        items as List of String
        check as Predicate of String
      <- result as Integer: 0

      for item in items
        if check(item)
          result++

  defines program

    ParameterGenusDemo()
      stdout <- Stdout()

      names <- List() of String
      names += "Alice"
      names += "Bob"
      names += "Charlie"

      //Dynamic function matching Predicate of String
      minLength <- 4

      longCheck <- (minLen: minLength) is Predicate of String as pure function
        r: t? and length t >= minLen

      longCount <- countMatching(names, longCheck)
      stdout.println(`Long names: ${longCount}`)

      shortCheck <- (maxLen: minLength) is Predicate of String as pure function
        r: t? and length t < maxLen

      shortCount <- countMatching(names, shortCheck)
      stdout.println(`Short names: ${shortCount}`)

Common mistakes

E06270 — The parameter expects a Predicate (function genus), not a Stdout instance. Passing the wrong kind of construct as a parameter causes a parameter mismatch. See ek9 -h E06270 for details.

Incorrect:

countMatching(names, stdout)

Correct:

countMatching(names, longCheck)
Other ways to ask this
  • What triggers E50090 GENUS_MISMATCH?
  • Why can't I pass a class where a function is expected?
  • What is parameter genus in EK9?
  • How does EK9 validate construct kinds?

Coming from another language?

Java: interface vs class distinction at compile time. Python: duck typing, no genus check. Rust: trait vs struct distinction. Go: interface vs struct. EK9: genus-level validation ensures fundamental construct kind matches.

Keywords: class, genus, parameter, construct, E50090, trait, mismatch, record, function, kind