I tried to extend List and got E05030. How do I wrap a collection instead?

← Debugging and Troubleshooting · Ref: Q1044

Built-in generic types (List, Dict, Optional, Result, PriorityQueue, MutexLock, DictEntry) are permanently closed. They cannot be extended. This is by design — extending collections mixes container behaviour with application logic.

The fix is composition: hold the collection as a property and expose only the operations you need.

BEFORE (E05030 error):

  UniqueNames extends List of String   closed — cannot extend

AFTER (composition):

  UniqueNames
    items as List of String?
    add()
      -> name as String
      if not items.contains(name)
        items += name
    count() as pure
      <- rtn as Integer: items.length()

Benefits of composition:
- You control the API — only expose what makes sense
- The internal collection type can change without affecting callers
- Works with any built-in type, not just open ones
- Your type gets its own operators (?, $, etc.)

Run 'ek9 -h E05030' for the compiler explanation.

See Q1043 for E05030 on user-defined types. See Q128 for why collections are closed. See Q212 for composition over inheritance.

Example

defines module qa.debugging.diagnoseclosedbuiltin

  defines class

    UniqueNames
      items as List of String?

      UniqueNames()
        items: List() of String

      add()
        -> name as String
        if not items.contains(name)
          items += name

      count() as pure
        <- rtn as Integer: items.length()

      override operator ? as pure
        <- rtn as Boolean: items?

      operator $ as pure
        <- rtn as String: `UniqueNames(${count()} items)`

  defines program

    ClosedBuiltinDemo()
      stdout <- Stdout()

      names <- UniqueNames()
      names.add("Alice")
      names.add("Bob")
      names.add("Alice")
      stdout.println(`${names}`)

Common mistakes

E05030 — List is a closed built-in type. Use composition: hold a List property and delegate methods to it.

Incorrect:

    UniqueNames extends List of String

Correct:

    UniqueNames
      items as List of String?
Other ways to ask this
  • Why can't I extend List of String in EK9?
  • How do I create a custom list type without inheritance?
  • Diagnose E05030 on a built-in generic type

Coming from another language?

Java allows extending ArrayList. EK9 collections are closed. Use composition to create custom collection wrappers.

Keywords: extend, builtin, wrap, closed, E05030, list, composition