E10031: 'T()' in the generic type needs a public no-argument constructor on the type used to parameterise it
What went wrong
The generic type calls 'T()' - a no-argument construction of its type parameter - but the type used to parameterise it has no PUBLIC no-argument constructor.
How to fix it
- Add a public no-argument constructor to the type you are passing as T: MyType() as pure ...
- Or make an existing private no-argument constructor public.
- Or parameterise with a type that already has one.
Rationale
Generic code is monomorphised against the concrete type, so 'T()' becomes a real construction of that type at the call site. The constructor therefore has to be reachable from the generic's own code - a private one is not.
Common causes
- The type declares 'default private Ctor()' to make itself non-instantiable
- Only parameterised constructors were declared, so no no-argument one exists
- The no-argument constructor exists but is not public
See also
Examples
Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.
Fails with E10031: 'T()' in the generic type needs a public no-argument constructor on the type used to parameterise it
#!ek9 defines module error.e10031.bad defines class PureHolder of type T item as T? PureHolder() as pure item :=? T() PureHolder() as pure -> initial as T item :=? initial default operator ? PrivateNoArgThing content as String? default private PrivateNoArgThing() as pure PrivateNoArgThing() as pure -> initial as String content :=? initial default operator ? defines function tryPrivateNoArg() held <- PureHolder() of PrivateNoArgThing require held?
Fixed: Give the type a public no-argument constructor for T()
#!ek9 defines module error.e10031.good defines class PureHolder of type T item as T? PureHolder() as pure item :=? T() PureHolder() as pure -> initial as T item :=? initial default operator ? PublicNoArgThing content as String? PublicNoArgThing() as pure content :=? "default" PublicNoArgThing() as pure -> initial as String content :=? initial default operator ? defines function tryPublicNoArg() held <- PureHolder() of PublicNoArgThing require held?
Found a problem?
If EK9 reported E10031 for a program you think is valid, or did not report it where it should have, use the compiler form with the smallest program that shows it. If this page is wrong or unclear, use the documentation form. Both are at github.com/ek9lang/ek9-issues.