E10031: 'T()' in the generic type needs a public no-argument constructor on the type used to parameterise it

Category: E10xxx IR generation · Reported in phase: POST_RESOLUTION_CHECKS · Classification: CONSTRUCTOR_NOT_ACCESSIBLE_FOR_PARAMETERISATION · All error codes

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

  1. Add a public no-argument constructor to the type you are passing as T: MyType() as pure ...
  2. Or make an existing private no-argument constructor public.
  3. 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

  • E10032: 'T()' needs a pure constructor in a pure generic context
  • E06040: generic type missing required constructors
  • E06130: constrained type missing constructors

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.