E06040: a generic type requires 2 constructors, default and inferred type
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:
- Default constructor: 'default Container()' - creates empty/unset instance
- Value constructor: takes T parameter for type inference
How to fix it
- Add 'default ClassName()' for empty instantiation
- Add constructor with T parameter if type inference is needed
- 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
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:
- Q643: Why must a generic class have both a default and an inferred-type constructor?
- Q683: What are the complete constructor rules for generic types?
- Q1071: Why does the compiler require two constructors for my generic class?
- Q1182: Define a generic Pair class with two type parameters and proper field initialisation.
- Q1373: Do built-in generic types and my own generic types behave the same when I pass a callback over a collection?
- Q1374: Can I store my own generic type inside a built-in container like List, Optional, Result or Dict?