Why can't I assign the result of a void function in EK9?

← Operators and Expressions · Ref: Q941

Void functions produce no value. Attempting to assign their result to a variable triggers E10010.

VOID FUNCTIONS HAVE NO RETURN

A function without a <- return declaration is void:

  logMessage()
    -> msg as String
    stdout.println(msg)       // No <- so this is void

You CANNOT assign void:

  result <- logMessage("hi")  // ERROR E10010 — nothing to assign

FUNCTIONS THAT RETURN VALUES

A function with <- produces a value you can capture:

  formatMessage() as pure
    -> msg as String
    <- rtn as String: `[LOG] ${msg}`
  result <- formatMessage("hi")   // CORRECT — function returns String

VOID IS FOR SIDE EFFECTS

Void functions perform actions: printing, writing files, sending messages. They are called as statements, not in expressions:

  logMessage("starting")        // CORRECT — standalone call
  sendAlert("warning")          // CORRECT — standalone call

See Q597 for function parameters and returns. See Q596 for function vs method.

Example

defines module qa.operators.voidnotassign

  defines function

    formatEntry() as pure
      -> msg as String
      <- rtn as String: `[INFO] ${msg}`

  defines program

    VoidAssignDemo()
      stdout <- Stdout()

      //Functions with <- return values that CAN be assigned
      formatted <- formatEntry("server started")
      stdout.println(formatted)

      //Void calls are standalone statements
      stdout.println("Direct output is a void call")

      //Multiple return values demonstrated
      first <- formatEntry("step one")
      second <- formatEntry("step two")
      stdout.println(first)
      stdout.println(second)

Common mistakes

E10010 — A void call returns nothing, so its result cannot be assigned to a variable. See ek9 -h E10010 for details.

Incorrect:

voidResult <- stdout.println("Direct output is a void call")

Correct:

stdout.println("Direct output is a void call")
Other ways to ask this
  • What triggers E10010 in EK9?
  • What happens when I try to store void in a variable?
  • Can a void function return a value in EK9?

Coming from another language?

Java: void methods cannot be assigned. C: void functions cannot be assigned. Python: None returned implicitly, assigning prints gives None. Rust: () unit type returned. EK9: void means no return, cannot assign.

Keywords: value, void, side, function, E10010, assign, return, effect