Lambdas |
Top Previous Next |
Lambda expressions are a popular shorthand syntax for anonymous methods. In Pas2JS, you can use Lambda Expressions and anonymous methods together! Lambda Function are used when the code expects an anonymous function and when the code implementing this function is short.
Given a function like this: Code example: anonymous methods reference to function type
There are three main parts of the function.
Parameters are enclosed in parentheses. Multiple statements and parameters can be used, but much of the simplification goes away. Parameterless functions and procedures can also be used. Usually, an empty parameter list is provided with a pair of parentheses.
While functions are usually used with Lambda expressions, procedures can be used too. If the expression does not evaluate to anything, it generally requires that the result not be assigned to anything. In C# and to some extent in Pas2JS, the type of the parameters and the function result can be determined very effectively by the compiler based on context, alleviating the need to specify these types. This is called "Type Inference" and further simplifies the code.
Because there is only one line and it is the result assignment, the assignment to result can be omitted, leaving just an expression to be evaluated. Hence, the "result :=" is removed.
Here are examples in Pas2JS.
Code example: Anonymous methods with lambdas
Lambda Statements: The statement form is used when you want to put more than one statement inside the lambda. The syntax for lambda statement is:
A lambda statement can also implement a function; in that case you should return the result in a normal Delphi way by assigning to a Result pseudo-variable.
Code example: Anonymous functions with lambdas
Lambdas are also very helpful when defining event handlers.
Code example: anonymous method version 1
Code example: anonymous method version 2
Code example: anonymous method with lambdas ver 1
Code example: anonymous method with lambdas ver 2
var repeater := TW3EventRepeater.Create(
Anonymous method calls some function in the code and returns its result. (False will trigger another event after the timeout (5000 ms) and True will stop the repeater.) Let’s rewrite this code using a lambda function.
var repeater :=
As you can see, there’s no need to declare the parameter (Sender) type and the function result type; Smart will detect them automatically. Even more, as we don’t use the Sender parameter, we can drop the parameter list and use an even shorter form.
var repeater :=
Following example sets up a repeater that calls a method MyProc every three seconds.
var repeater :=
You can use variables inside a lambda statement, but only if they are declared inline. Lambdas are an excellent addition to the language, as they provide for a more compact code. For example, in Smart 1.0 the W3Layout unit used the following code:
ResizeChildren(FClientArea.Height, [TAlign.Top, TAlign.Bottom],
In Smart 1.1, this was simplified to:
ResizeChildren(FClientArea.Height, [TAlign.Top, TAlign.Bottom],
|