E06030: type is generic, but for type inference to work; the number of generic and constructor parameters must be the same

Category: E06xxx Type hierarchy checks · Reported in phase: SYMBOL_DEFINITION · Classification: GENERIC_TYPE_CONSTRUCTOR_INAPPROPRIATE · All error codes

What went wrong

A constructor of a generic type takes a number of parameters that is neither zero nor the number of its type parameters. A generic's constructors are its no-argument constructor and the INFERRING constructor, which takes exactly one parameter per type parameter, in the same order: 'Box(21)' infers 'Box of Integer' from it, and 'Pair("answer", 42)' infers 'Pair of (String, Integer)'.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 642 (How does constructor-based type inference work for generi...)
  • ek9 -q 196 (How do I define generics with multiple type parameters in...)

[FOR TWO-PARAMETER GENERICS] A generic 'Pair of type (K, V)' has an inferring constructor taking a K and then a V, declared as ONE '->' block of two parameters (good_pair.ek9 in the [EXAMPLE] section below).

How to fix it

  1. Give the inferring constructor exactly one parameter per type parameter
  2. Type each as its type parameter (T, K, V, ...), in the same order
  3. Any other way of building the object belongs in a method or function, not a constructor

The [EXAMPLE] section below shows a one-parameter generic with a two-parameter constructor, the generic with its no-argument and inferring constructors, and a two-parameter Pair.

Common causes

  • Using concrete type instead of generic type parameter
  • Constructor param order doesn't match generic param order
  • Missing constructor parameter for one of the generic types

See also

  • E06030: constructor/generic alignment issue (this error)
  • E06040: missing required constructor
  • E06050: constructor argument type mismatch

Examples

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

Fails with E06030: type is generic, but for type inference to work; the number of generic and constructor parameters must be the same

#!ek9
defines module error.e06030.bad

  defines class

    SomeGenericType of type T
      aField as T?

      default SomeGenericType()

      SomeGenericType()
        -> param as T
        aField :=: param

      //Failure 1
      //This is not allowed, as it is expected a single parameter will give the compiler
      //the type to infer for the generic nature of the generic/template class.
      SomeGenericType()
        ->
          param1 as T
          param2 as T

        aField :=: param1 + param2

Fixed: A generic type has one constructor taking each type parameter once; combine values in a method

#!ek9
defines module error.e06030.good

  defines class

    SomeGenericType of type T
      aField as T: T()

      default SomeGenericType()

      SomeGenericType()
        -> param as T
        aField :=: param

      contents()
        <- rtn as T: aField

      default operator ?

Fixed: Two type parameters: the inferring constructor takes one of each, in order

#!ek9
defines module error.e06030.good.pair

  defines class
    Pair of type (K, V)
      key as K: K()
      content as V: V()

      default Pair()

      Pair()
        ->
          initialKey as K
          initialContent as V
        key :=: initialKey
        content :=: initialContent

      describe()
        <- rtn as String: `${key}=${content}`

      default operator ?

  defines program
    ShowPair()
      stdout <- Stdout()
      //Pair of (String, Integer) is inferred from the two arguments
      entry <- Pair("answer", 42)
      stdout.println(entry.describe())

Questions and answers

These Q&A entries show E06030 and its fix: