E06110: use of a constructor in a generic/template that uses a function is not supported

Category: E06xxx Type hierarchy checks · Reported in phase: POST_RESOLUTION_CHECKS · Classification: CONSTRUCTOR_WITH_FUNCTION_IN_GENERIC · All error codes

What went wrong

Constructors in generic types cannot have function types as parameters. This restriction ensures proper type inference - the compiler cannot reliably infer both the generic type T AND the function signature.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 833 (Why can't I use a function type to parameterize a generic...)

Rationale

Function type parameters in generic constructors create ambiguity. When the compiler sees Container(someFunction), it cannot determine both the generic type T AND the function signature simultaneously.

How to fix it

  1. Remove function parameters from generic constructors
  2. Add setHandler() method to inject functions after construction
  3. Or use a non-function wrapper type for the parameter

Common causes

  • Wanting dependency injection via constructor
  • Coming from languages without this restriction
  • Trying to make functions first-class in generic constructors

See also

  • E06110: function parameter in generic constructor (this error)
  • E06120: function operator usage in generic context
  • E06080: function type used as constraint

Examples

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

Fails with E06110: use of a constructor in a generic/template that uses a function is not supported

#!ek9
defines module error.e06110.bad

  defines constant
    DrinkingAge <- 21

  defines class

    Factory of type T

      default Factory()

      default Factory()
        -> item as T

      //Assumes T can be constructed - a function cannot
      fresh()
        <- rtn as T: T()

  defines function

    isAdult()
      -> age as Integer
      <- rtn as Boolean: age >= DrinkingAge

  defines function

    useFactory()
      factory <- Factory() of isAdult
      require factory?

Fixed: Parameterise with a type that can be constructed, or use a generic that does not construct T

#!ek9
defines module error.e06110.good

  defines constant
    DrinkingAge <- 21

  defines class

    Factory of type T

      default Factory()

      default Factory()
        -> item as T

      fresh()
        <- rtn as T: T()

    Pair of type T

      default Pair()

      default Pair()
        -> item as T

      bothSet()
        ->
          first as T
          second as T
        <- rtn as Boolean: first? and second?

  defines function

    isAdult()
      -> age as Integer
      <- rtn as Boolean: age >= DrinkingAge

  defines function

    useBoth()
      <- rtn as Boolean?
      factory <- Factory() of String
      pair <- Pair() of isAdult
      rtn: factory.fresh()? and pair.bothSet(isAdult, isAdult)

Questions and answers

These Q&A entries show E06110 and its fix: