Lambda Expressions
A lambda expression is a procedure without a name that you can use as an alternative to a named event handler.
You can write a lambda expression on one line or as a block of statements.
Lambda expressions can use the semantics of one of the statements Function
or Sub
.
A lambda expression with the semantics of Sub
has access to the local scope.
You make a lambda expression with one of the keywords Function
or Sub
, almost the same as the statements that use these keywords.
The difference is that lambda expressions are a part of a statement.
In the example that follows, lambda expressions add to a value. The example shows the one-line and block constructs for a function.
Var add1 = Function(x) x + 1
Var add2 = Function(x)
Return x + 2
End Function
Print add1(1)
Print add2(2)
2 4 |
Lambda expression syntax
The syntax of a lambda expression is almost the same as a typical procedure. The differences are as follows:
- A lambda expression does not have a name.
- Lambda expressions cannot have modifiers, for example,
@Abstract
or@MustUse
. - The body of the one-line construct must be an expression that returns a value, not a statement.
- The one-line construct is not completed with one of the clauses
End
,End Function
, orEnd Sub
. - Optional parameters are not permitted.
- Generic parameters are not permitted.
Context
TODO