Can I assign an abstract function as a delegate in EK9?
← Functions and Methods · Ref: Q848
You cannot assign an abstract function reference as a delegate. Abstract functions have no implementation body, so there is nothing to execute. You must assign a concrete function that provides an actual implementation.
WHY ABSTRACT FUNCTIONS CANNOT BE DELEGATES
A delegate is a callable reference. Abstract functions declare a signature but have no body. Assigning one would create a delegate that cannot be called, which is a compile-time error rather than a runtime failure.
CORRECT PATTERN
Define an abstract function, implement it with a concrete subtype, and assign the concrete version:
Transformer as abstract -> input as String <- rtn as String? UpperTransformer() is Transformer -> input as String <- rtn as String: input.upperCase() handler as Transformer: UpperTransformer
INCORRECT PATTERN
handler as Transformer: Transformer // E50070: abstract, no body
See Q100 for function basics. See Q102 for function composition. See Q106 for higher-order functions.
Example
defines module qa.functions.abstract.assignment defines function <?- Abstract function declaring the contract. Cannot be used directly as a delegate. -?> Transformer as abstract -> input as String <- rtn as String? <?- Concrete implementation of Transformer. This can be assigned as a delegate. -?> UpperTransformer() is Transformer -> input as String <- rtn as String: input.upperCase() defines program AbstractFunctionDemo() stdout <- Stdout() //Correct: assign concrete function handler as Transformer: UpperTransformer if handler? stdout.println(handler("hello"))
Common mistakes
E50070 — Abstract functions have no body and cannot be assigned as delegates. Use a concrete implementation instead. See ek9 -h E50070 for details.
Incorrect:
handler as Transformer: Transformer
Correct:
handler as Transformer: UpperTransformer
Other ways to ask this
- What is E50070 in EK9?
- Why can I not use an abstract function as a delegate?
- How do I assign a function reference in EK9?
- What is the correct way to delegate to a function?
Coming from another language?
Java: abstract classes cannot be instantiated but interfaces can be lambdas. Python: ABCMeta prevents instantiation at runtime. Kotlin: abstract functions cannot be referenced directly. Rust: trait objects require concrete implementations. EK9: E50070 prevents assigning abstract functions as delegates at compile time.
Keywords: callable, abstract, function, assignment, implementation, E50070, reference, concrete, delegate