Why does my abstract function get E08180 on the return value?

← Functions and Methods · Ref: Q1072

Abstract function return values must be declared with ? (unset marker) or initialised with a default value.

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 — Option 1 (? marker — returns Optional-like):

  transformer() as abstract
    -> input as String
    <- rtn as String?

AFTER — Option 2 (default value):

  transformer() as abstract
    -> input as String
    <- rtn as String: String()

Both work. Use ? when the function might legitimately return unset. Use a default value when a fallback makes sense.

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. for override on operators.

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) or a default initialiser. Use '<- rtn as String?' to allow unset returns.

Incorrect:

    transformer() as abstract
      -> input as String
      <- rtn as 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 ? or initialised.

Keywords: unset, function, initialise, return, E08180, abstract