Why can't I construct an abstract type directly in EK9?
← Code Quality · Ref: Q821
EK9 prevents direct construction of abstract types at compile time. Abstract types define contracts — they must be extended with concrete implementations.
THE RULE
You cannot call the constructor of an abstract class, function, or generic parameterized with an abstract type:
Shape as abstract // abstract — no direct construction s <- Shape() // ERROR: E10030
HOW TO FIX
- Create a concrete subclass and construct that instead
- Use a dynamic class/function for inline implementation
- If using generics, parameterize with a concrete type
WHY THIS MATTERS
Abstract types may have abstract methods with no implementation. Constructing them would create an object that cannot respond to all its methods. EK9 catches this at compile time rather than allowing it to fail at runtime.
See Q97 for class vs record. See Q96 for inheritance.
Example
defines module qa.quality.abstract.construction defines class Shape as abstract default operator ? Circle extends Shape default Circle() default operator ? defines program AbstractConstructionDemo() stdout <- Stdout() // === CORRECT: construct concrete subclass === shape <- Circle() stdout.println(`Shape created: ${shape?}`)
Common mistakes
E50080 — Shape is abstract and cannot be constructed directly. Use a concrete subclass like Circle instead. See ek9 -h E50080 for details.
Incorrect:
shape <- Shape()
Correct:
shape <- Circle()
Other ways to ask this
- What is E10030 CONSTRUCTOR_USED_ON_ABSTRACT_TYPE?
- Why does EK9 reject new MyAbstractClass()?
- How do I fix abstract type construction errors in EK9?
Coming from another language?
Java: cannot instantiate abstract class — runtime InstantiationError if attempted via reflection. Python: ABCMeta raises TypeError at runtime. Rust: traits cannot be constructed (no inheritance). Go: interfaces have no constructors. EK9: compile-time error for abstract construction.
Keywords: abstract, concrete, E10030, implementation, type, constructor, construction