How does EK9 validate the number of type parameters for generics?

← Generics · Ref: Q654

EK9 validates that the number of type arguments matches the number of type parameters declared on a generic type. Providing too few or too many type arguments produces E06020.

SINGLE PARAMETER TYPES

Types declared 'of type T' need exactly one type argument:

  List of String         // correct: 1 param, 1 arg
  Optional of Integer    // correct: 1 param, 1 arg

MULTI PARAMETER TYPES

Types declared 'of type (K, V)' need exactly two:

  Dict of (String, Integer)  // correct: 2 params, 2 args

MISMATCH DETECTED

E06020 is raised when counts do not match:

  Dict of String          // ERROR: needs 2, got 1
  List of (String, Integer)  // ERROR: needs 1, got 2

PARAMETER SUPPLY

Always match the generic declaration:

  MyType of type T         -> supply 1 type
  MyType of type (A, B)    -> supply 2 types
  MyType of type (X, Y, Z) -> supply 3 types

See Q196 for multi-parameter generics. See Q198 for built-in generics. See Q642 for constructor inference.

Example

defines module qa.genericsdeep.parametercount

  defines class

    <?-
      Single parameter generic: needs exactly 1 type argument.
    -?>
    Wrapper of type T
      item as T?

      Wrapper() as pure
        item :=? T()

      Wrapper() as pure
        -> inputItem as T
        item :=? inputItem

      default operator ?

    <?-
      Two parameter generic: needs exactly 2 type arguments.
    -?>
    Association of type (K, V)
      theKey as K?
      theValue as V?

      Association() as pure
        theKey :=? K()
        theValue :=? V()

      Association() as pure
        ->
          k as K
          v as V
        theKey :=? k
        theValue :=? v

      default operator ?

  defines program

    ParameterCountDemo()
      stdout <- Stdout()

      // === SINGLE PARAM: one type argument ===

      intWrap <- Wrapper(42)
      stdout.println(`Int wrapper: ${intWrap?}`)

      strWrap <- Wrapper() of String
      stdout.println(`String wrapper: ${strWrap?}`)

      // === TWO PARAMS: two type arguments ===

      assoc <- Association("key", 100)
      stdout.println(`Association: ${assoc?}`)

      emptyAssoc <- Association() of (String, Float)
      stdout.println(`Empty association: ${emptyAssoc?}`)

      // === BUILT-IN GENERICS FOLLOW SAME RULES ===

      names <- List() of String
      names += "Alice"
      stdout.println(`List count: ${length names}`)

      mapping <- Dict() of (String, Integer)
      stdout.println(`Dict set: ${mapping?}`)

Common mistakes

E06020 — The number of type arguments must match the number of type parameters declared on the generic. Association declares two parameters (K, V), so providing only one triggers E06020. See ek9 -h E06020 for details.

Incorrect:

Association of type K

Correct:

Association of type (K, V)
Other ways to ask this
  • What is E06020 incorrect number of parameters for generic?
  • What happens if I provide wrong number of type arguments?
  • How does EK9 check generic parameter counts?

Coming from another language?

Java: compiler checks type argument count against declaration. C++: template parameter count validated. Rust: generic parameter count validated. Go: type parameter count validated (Go 1.18+). Kotlin: same as Java. EK9: E06020 raised for parameter count mismatch, checked early in compilation.

Keywords: type, count, type-parameter, E06020, generic, validation, parameter, argument, mismatch