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 expressionDescription
The void command takes an expression as its parameter, evaluates it completely, and then discards the result. This is particularly useful for:
Executing functions solely for their side effects
Suppressing unwanted return values
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:
Event handlers - When executing code in response to events
Initialization code - When setting up components at startup
Side effect functions - When calling functions primarily for their side effects
Cleanup operations - When performing cleanup that returns values you don't need
Notes
The
voidcommand always evaluates its parameters fullyIt always returns
null(though this return value is typically ignored)Using
voidcan make your code more explicit about your intentionsIt's a good practice to use
voidwhen you're deliberately ignoring return values
Last updated
Was this helpful?