E06040: a generic type requires 2 constructors, default and inferred type

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

What went wrong

A generic type is missing required constructors for proper instantiation. Generic types typically need specific constructors for the compiler to properly create parameterized instances.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 643 (Why must a generic class have both a default and an infer...)

Required constructor patterns

For most generic types, define:

  1. Default constructor: 'default Container()' - creates empty/unset instance
  2. Value constructor: takes T parameter for type inference

How to fix it

  1. Add 'default ClassName()' for empty instantiation
  2. Add constructor with T parameter if type inference is needed
  3. Ensure at least one constructor exists

Rationale

Generic types need constructors for:

  • Empty instantiation: Container() of String
  • Type inference: Container("value") -> Container of String inferred

Common causes

  • Forgetting to define any constructors
  • Assuming default constructor is auto-generated (it's not in generics)
  • Only defining parameterized constructor, missing default

See also

  • E06040: missing required constructor (this error)
  • E06030: constructor/generic alignment issue
  • 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 E06040: a generic type requires 2 constructors, default and inferred type

#!ek9
defines module error.e06040.bad

  defines class

    //A generic type needs a no-argument constructor AND one taking each type parameter
    Box of type T
      default Box()

      check()
        -> item as T
        <- rtn as Boolean: item?

Fixed: Declare no constructors and let the compiler make both, or declare both

#!ek9
defines module error.e06040.good

  defines class

    AutoBox of type T
      check()
        -> item as T
        <- rtn as Boolean: item?

    ManualBox of type T
      default ManualBox()

      default ManualBox()
        -> item as T

      check()
        -> item as T
        <- rtn as Boolean: item?

Questions and answers

These Q&A entries show E06040 and its fix: