E06130: constraining type constructors must exist on parameterizing type

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

What went wrong

When using constrained generic types, the parameterizing type must have ALL constructors required by the constraining type. If the constraint has a default constructor, T must also have one.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 651 (How must constrained type constructors match the paramete...)

How to fix it

  1. Add missing constructors to the type you're using as T
  2. Check what constructors the constraining type defines
  3. Ensure T has at least those constructors

Rationale

Generic code using constraints may call T() to create instances. If the constraining type has such constructors, T must also have them for the generic code to work.

Common causes

  • Type parameter missing default constructor
  • Constraining type requires constructors T doesn't have
  • Forgetting that constraint imposes constructor requirements

See also

  • E06130: constrained type missing constructors (this error)
  • E06040: generic type missing required constructors
  • E06080: function used as constraint (different issue)

Examples

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

Fails with E06130: constraining type constructors must exist on parameterizing type

#!ek9
defines module error.e06130.bad

  defines record

    R2 as open
      total as Integer?

      R2() as pure
        total :=? 1

      R2() as pure
        -> initial as Integer
        total :=? Integer(initial)

      R2() as pure
        -> other as R2
        total :=? Integer(other.total)

      default operator ?

      operator + as pure
        -> other as R2
        <- rtn as R2: R2(total + other.total)

    R3 is R2

      R3() as pure
        total :=? 1

      R3() as pure
        -> initial as Integer
        super(initial)

      default operator ?

  defines function

    //T(...) below needs every T to have the constructor R2 has: T(-> other as R2)
    addAndWrap() of type T constrain by R2
      ->
        first as T
        second as T
      <- rtn as T: T(first + second)

    useR3()
      //R3 has no R3(-> other as R2) constructor
      adder <- addAndWrap() of R3
      require adder?

Fixed: Give the parameterising type every constructor the constraining type has

#!ek9
defines module error.e06130.good

  defines record

    R2 as open
      total as Integer?

      R2() as pure
        total :=? 1

      R2() as pure
        -> initial as Integer
        total :=? Integer(initial)

      R2() as pure
        -> other as R2
        total :=? Integer(other.total)

      default operator ?

      operator + as pure
        -> other as R2
        <- rtn as R2: R2(total + other.total)

    R4 is R2

      R4() as pure
        total :=? 1

      R4() as pure
        -> initial as Integer
        super(initial)

      R4() as pure
        -> other as R2
        super(other)

      default operator ?

  defines function

    //T(...) below needs every T to have the constructor R2 has: T(-> other as R2)
    addAndWrap() of type T constrain by R2
      ->
        first as T
        second as T
      <- rtn as T: T(first + second)

    useR4()
      adder <- addAndWrap() of R4
      require adder?

Questions and answers

These Q&A entries show E06130 and its fix:

Found a problem?

If EK9 reported E06130 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.