LAMBDA

Declare a lambda function

 

 

 

NSupported in Synergy .NET
[ASYNC] LAMBDA name ([param_id [, ...param_id]]) expression

or

[ASYNC] LAMBDA name ([param_id [, ...param_id]])
    BEGIN
    .
    .
    .
    END

Arguments

ASYNC

(optional) Lambda function will perform asynchronous processing.

name

The name of the lambda function. (a)

param_id

(optional) A parameter to pass to the lambda function.

expression

The expression to be evaluated.

Discussion

The LAMBDA statement provides a way to generate inline methods that have access to variables within the method in which the lambda is declared. A lambda function can take any number of parameters, and it returns the value of a single expression. It can contain any variables that are currently in scope, with the following exceptions:

The method generated by a lambda function is at the same scope level as the routine in which it is declared. For example, if the routine is a class routine, the method generated by the lambda function will be a class routine.

Note

When accessing local variables, be aware that the lifetime of those variables changes from local scope to the scope outside of the method in which they are declared. If you’re writing to local variables in a threading scenario, such as WPF, make sure the access is thread-safe.

You can use a lambda function in the following ways:

mymethod(myadd)
addhandler(classvar.event1, myadd)
delegate deleg1, int
    parm1, int
enddelegate
record
    dvar, @deleg1
proc
dvar = myadd
x = dvar(5)
x = ((deleg1)myadd)(5)

If a lambda is to perform asynchronous processing, it must be marked as ASYNC. Asynchronous processing means that a process can occur independently of other processes instead of being required to occur in direct succession. Within an asynchronous lambda, you may specify one or more AWAIT statements, which wait for the corresponding asynchronous task to complete. See Asynchronous processing (.NET) and AWAIT for more information.

Inline lambdas

Lambdas can also be inline functions. An inline lambda can be used anywhere a regular lambda can be used (for example, data fields in routines, parameters, and class fields), and it has the following syntax:

lambda(param_id,...) {expression},...

In the following example, an unnamed lambda is created and a pointer to it is stored in fld1.

namespace ns1
  delegate deleg1, int
      parm1, string
  enddelegate
  class class1
    public static fld1, @deleg1, lambda(arg) {arg.Length+101}
  endclass
endnamespace

You can then invoke the lambda like this:

x = ns1.class1.fld1("hi")

Here’s another example for a data field assignment:

data var1, @deleg1
; Assignment
var1 = lambda(arg) {arg.Length+20}
x = var1("go")

Examples

record
    i, int
proc
    lambda myadd(x, y)
      begin
        mreturn x + y
      end
end