How does EK9 validate generic constructors and sealed type allow only lists?

← Code Quality · Ref: Q744

EK9 validates two specific type constraint patterns at compile time.

INCOMPATIBLE GENUS CONSTRUCTOR (E05200)

When instantiating a generic type, the constructor arguments must be compatible with the type parameters. If the generic type requires a specific kind of constructor (e.g., a copy constructor from the type parameter), the actual arguments must satisfy that constraint.

ABSTRACT IN ALLOW ONLY (E05250)

Sealed traits use 'allow only' to restrict which concrete types can implement them. Abstract classes cannot appear in this list because they can never be instantiated as concrete runtime types. Only concrete classes that can actually exist at runtime belong in the allow only list.

CORRECT PATTERNS

For generics, ensure constructor arguments match type parameter requirements. For sealed traits, list only concrete classes in the allow only clause.

See Q679 for sealed allow only. See Q642 for generic constructor inference.

Example

defines module qa.codequality.genericsealedconstraints

  defines class

    ConcreteAlpha
      doWork()
        content <- "Alpha"
        require content?

      default operator

    ConcreteBeta
      doWork()
        content <- "Beta"
        require content?

      default operator

  defines trait

    //Correct: only concrete classes in allow only list
    Processable allow only ConcreteAlpha, ConcreteBeta
      process() as abstract

  defines program

    GenericSealedDemo()
      stdout <- Stdout()

      //Correct generic usage
      names <- List() of String
      names += "Alice"
      names += "Bob"
      stdout.println(`Names: ${length names}`)

Common mistakes

E50010 — AbstractWorker does not exist in this module. Adding an unresolved type to the allow only list triggers E50010. See ek9 -h E50010 for details.

Incorrect:

Processable allow only ConcreteAlpha, AbstractWorker, ConcreteBeta

Correct:

Processable allow only ConcreteAlpha, ConcreteBeta

E06010 — List is a generic type that requires a type parameter. Use 'List() of String' for explicit typing. See ek9 -h E06010 for details.

Incorrect:

names <- List()

Correct:

names <- List() of String
Other ways to ask this
  • What is E05200 incompatible genus constructor in EK9?
  • What is E05250 abstract in allow only in EK9?
  • Why does EK9 reject abstract classes in sealed trait allow only lists?

Coming from another language?

Java: generic constraints via bounded wildcards, no sealed trait equivalent until Java 17. Kotlin: sealed classes restrict subclassing. Rust: trait bounds on generics. Swift: no sealed traits. EK9: compile-time enforcement of both generic constructor compatibility and sealed trait allow only lists.

Keywords: quality, constructor, only, constraint, sealed, E05200, allow, generic, E05250, abstract