E05270: a class with 'allow only' must be declared 'as open'

Category: E05xxx Explicit type symbol definition · Reported in phase: EXPLICIT_TYPE_SYMBOL_DEFINITION · Classification: SEALED_CLASS_MUST_BE_OPEN · All error codes

What went wrong

A class declares 'allow only' but is not declared 'as open'. The 'allow only' clause restricts which classes can extend this class, but a class that is not 'as open' cannot be extended at all. This creates a contradictory declaration — the 'allow only' list would never be used because no class can extend it.

EK9 classes are closed by default (like Kotlin). To allow extension, the class must be explicitly declared 'as open'. When combined with 'allow only', this creates a sealed class — open to extension, but only by the named classes.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 607 (Must a class with 'allow only' be declared as open?)
  • ek9 -q 679 (How do sealed classes with allow only work in EK9?)
  • ek9 -q 608 (How do sealed classes differ from sealed traits in EK9?)
  • ek9 -q 611 (What is the full set of type hierarchy checks EK9 performs?)

How to fix it

Add 'as open' to the class declaration. A sealed class must be extensible for the 'allow only' restriction to have meaning.

Alternatively, if the class should not be extended at all, remove the 'allow only' clause — a closed class already prevents all extension.

Common causes

  • Forgetting that EK9 classes are closed by default
  • Adding 'allow only' to a class that was not previously extensible
  • Copy-pasting trait syntax (traits are always open) to a class

See also

  • E05240: type is not permitted to extend/implement this sealed type
  • E05250: abstract type cannot be in 'allow only' list
  • E05030: not open to be extended

Examples

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

Fails with E05270: a class with 'allow only' must be declared 'as open'

#!ek9
defines module error.e05270.bad

  defines class

    //Shape names who may extend it, but is closed: nothing may
    Shape allow only Circle, Square
      area()
        <- rtn as Float: 0.0

    Circle
      area()
        <- rtn as Float: 3.14

    Square
      area()
        <- rtn as Float: 1.0

Fixed: Open the class, and let the classes it names extend it

#!ek9
defines module error.e05270.good.fix

  defines class

    //Shape names who may extend it, but is closed: nothing may
    Shape allow only Circle, Square as open
      area()
        <- rtn as Float: 0.0

    Circle extends Shape
      override area()
        <- rtn as Float: 3.14

    Square extends Shape
      override area()
        <- rtn as Float: 1.0

Fixed: Declare the sealed class 'as open'; 'allow only' then limits who extends it

#!ek9
defines module error.e05270.good

  defines class

    Shape allow only Circle, Square as open
      area()
        <- rtn as Float: 0.0

    Circle extends Shape
      override area()
        <- rtn as Float: 3.14

    Square extends Shape
      override area()
        <- rtn as Float: 1.0

Questions and answers

These Q&A entries show E05270 and its fix: