Skip to main content

Callback

The Callback type represents a function that can be called dynamically. Unlike a normal function call, callbacks are dispatched at runtime rather than at compile time, and so they can only have a simple parameter list - no context variables and no named arguments, just a list of positional arguments.

// Block syntax
let callback1 = |name| {
return "Hello, " + name + "!"
}
let greeting = callback1("world") // assigns "Hello, world!" to greeting

// Expression syntax
let callback2 = |name| "Goodbye, " + name + "!"
greeting = callback1("Fred") // assigns "Goodbye, Fred!" to greeting

// Callbacks can be used to customize certain functions
let players = QueryPlayers.OrderBy(|x| x.PlayerName)

All subblock parameters are actually just callbacks.

In other programming languages, callbacks are sometimes called lambdas, delegates, arrow functions, closures, or anonymous functions.