Lambda Expressions are a bit like a codeblock. it is a block of code with optional parameters and a return value.
They are used to create anonymous functions.
Unlike a codeblock they do NOT inherit from a common class. They also cannot be stored inside the USUAL type.
Code that expects a lambda expression often declares a delegate that describes the parameter types and return value of the lambda expression.
Lambda expressions can also be used to declare an event handler.
The return value of the lambda expression is the last expression in the expression list, or the value returned by the return statement in the statement list.
{ [pars] => expression [, expressionlist] }
or
{ [pars] =>
statements // these are NOT separated with semi colons!
}
pars | Comma separated list of arguments. They can optionally be typed. |
Some examples:
// single expression, untyped parameters
{ a, b => a * b}
// single expression, typed parameters
{ a as int, b as int => a / b}
// typed parameters, expression list, last expression is return value
{ a as int, b as int => a := iif(a == 0, 1, a), b / a}
// statement list, no semi colons needed, return statement is return value
{ a as int, b as int =>
if a == 0
a := 1
endif
return b / a
}
// Register a click event handler. No need to type the args
// The signature is derived from the signature of the Click Event
btnOk:Click += { sender, args => SELF:Close() }