void expression

The void command evaluates its parameters but discards the result. It's useful when you want to execute a function or expression for its side effects without using the return value.

Syntax

void expression

Description

The void command takes an expression as its parameter, evaluates it completely, and then discards the result. This is particularly useful for:

  1. Executing functions solely for their side effects

  2. Suppressing unwanted return values

  3. Explicitly indicating that a return value is being ignored

Examples

Executing Functions

// Define a function with side effects
def logMessage(message) (
  log message
  return message  // Return value we don't need
)

// Execute the function and discard the return value
void logMessage("Hello, World!")

Executing Methods with Side Effects

Suppressing Unwanted Return Values

Executing Multiple Expressions

You can use void with multiple expressions by grouping them:

Using in Event Handlers

Use Cases

The void command is particularly useful in these scenarios:

  1. Event handlers - When executing code in response to events

  2. Initialization code - When setting up components at startup

  3. Side effect functions - When calling functions primarily for their side effects

  4. Cleanup operations - When performing cleanup that returns values you don't need

Notes

  • The void command always evaluates its parameters fully

  • It always returns null (though this return value is typically ignored)

  • Using void can make your code more explicit about your intentions

  • It's a good practice to use void when you're deliberately ignoring return values

Last updated

Was this helpful?