Why does my abstract function get E08180 on the return value?
← Functions and Methods · Ref: Q1072
An abstract function's return value must be declared with ? (unset marker). It cannot be left bare, and it cannot be given a default value either.
BEFORE (E08180 error):
transformer() as abstract -> input as String <- rtn as String
The problem: 'rtn as String' has no initial value and no ? marker. The compiler cannot guarantee it will be set.
AFTER (? marker):
transformer() as abstract -> input as String <- rtn as String?
NOT A FIX (default value, E07100):
transformer() as abstract -> input as String <- rtn as String: String() // ERROR E07100
An initialiser on the return value counts as an implementation, and an abstract function may not have one: E07100 (defined as default/abstract but an implementation has been provided). The '?' form is the only one that works; the functions that implement the abstract one decide what value to return.
For dynamic functions implementing the abstract, the return variable inherits the declaration:
myFn <- (prefix) is transformer as function rtn: prefix + input
See Q51 for abstract function types. See Q52 for dynamic functions.
Example
defines module qa.functions.abstractreturn defines function //Abstract with ? marker on return transformer() as abstract -> input as String <- rtn as String? defines program AbstractReturnDemo() stdout <- Stdout() //Dynamic function implementing the abstract prefix <- "Hello: " myFn <- (prefix) is transformer as function rtn: prefix + input result <- myFn("World") stdout.println(result)
Common mistakes
E08180 — Abstract function return values need ? (unset marker). Use '<- rtn as String?'; a default initialiser is not allowed either (E07100).
Incorrect:
transformer() as abstract -> input as String <- rtn as String
Correct:
transformer() as abstract -> input as String <- rtn as String?
E07100 — An initialiser on an abstract function's return value counts as an implementation, so it triggers E07100 - defined as default/abstract but an implementation has been provided. Declare the return as 'String?' instead.
Incorrect:
transformer() as abstract -> input as String <- rtn as String: String()
Correct:
transformer() as abstract -> input as String <- rtn as String?
Other ways to ask this
- How do I declare the return value on an abstract function?
- What is E08180 variable not initialised in an abstract function?
- Should I use String? or String() for abstract function returns?
Coming from another language?
Java: abstract methods declare return type only. Python: no return type. EK9: abstract function return variables must be declared with ? (an initialiser counts as a body, E07100).
Keywords: function, unset, abstract, E08180, return, initialise