Why can't I instantiate an abstract generic type like Iterator of String?

← Generics · Ref: Q841

Parameterizing an abstract generic type does NOT make it concrete. Iterator, Supplier, Predicate, Consumer, and other abstract function types remain abstract after parameterization.

WHY IT FAILS

  notAllowed <- Iterator() of String  // ERROR E10030

Iterator is abstract. Adding 'of String' specifies the type parameter but does not provide an implementation. You cannot call a constructor on an abstract type.

CORRECT ALTERNATIVES

1. Get an iterator from a concrete collection:

  names <- List() of String
  names += "Steve"
  iter <- names.iterator()

2. Use a dynamic function for abstract function types:

  greeter <- () is Supplier of String as function (rtn: "Hello")

3. Extend with a concrete implementation:

  Define a class that extends the abstract type and provides method bodies.

THIS APPLIES TO ALL ABSTRACT GENERICS

Iterator, Supplier, Consumer, Producer, Predicate, Acceptor, Function, Comparator, UnaryOperator, and all Bi- variants.

See Q642 for generic constructor inference. See Q194 for generic class basics.

Example

defines module qa.genericsdeep.abstract.generic.instantiation

  defines program

    AbstractGenericDemo()
      stdout <- Stdout()

      // === CORRECT: get iterator from a concrete List ===
      names <- List() of String
      names += "Steve"
      names += "Limb"

      iter <- names.iterator()
      stdout.println(`Iterator obtained: ${iter?}`)

      // === CORRECT: use concrete implementation for abstract function types ===
      names += "EK9"
      stdout.println(`List has ${length names} items`)

Common mistakes

E10030 — Iterator is abstract. Parameterizing it with 'of String' does not make it concrete. Get an iterator from a concrete collection like List instead. See ek9 -h E10030 for details.

Incorrect:

      iter <- Iterator() of String

Correct:

      iter <- names.iterator()
Other ways to ask this
  • What triggers E10030 CONSTRUCTOR_USED_ON_ABSTRACT_TYPE?
  • Why does Iterator() of String fail in EK9?
  • How do I get an iterator from a collection in EK9?

Coming from another language?

Java: Same rule — 'new Iterator<String>()' fails. Java allows anonymous classes: 'new Iterator<String>() { ... }'. Python: abc module catches abstract instantiation at runtime only. Kotlin: Same compile-time enforcement; uses SAM conversions for functional interfaces. Rust: Traits cannot be instantiated, only implemented. EK9: E10030 at compile time for all abstract generic types.

Keywords: generic, concrete, abstract, collection, iterator, instantiation, constructor, E10030