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 = callback2("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.
Ignoring some callback parameters
Use _ to make the callback ignore any parameters that you do not need.
let callback1 = |firstName, _, age| {
return "Our friend " + firstName + " is " + age + " years old."
}
let greeting = callback1("Fred", "Smith", 10) // assigns "Our friend Fred is 10 years old." to greeting
Declaring context parameters
Prefix any parameter with use to make it a context variable.
fn DisplayGreeting(firstName, [age]) {
Print { "Our friend " + firstName + " is " + age + " years old." }
}
fn Example() {
let callback1 = |firstName, _, use age| {
// the age parameter is passed implicitly through context
DisplayGreeting(firstName=)
}
callback1()
}
Awaiting a callback
Calling a callback waits for it to fully complete.
This includes waiting for any await calls that the callback does.
Unlike other programming languages,
you do not need to use the await keyword to wait when calling the callback.
This works because Easel is a stackful coroutine language,
which means it can suspend and resume the execution of any function at any point.
In other programming languages you need to specify resume points using the await keyword,
but not in Easel.