Skip to content

Expressions: Let expressions

A let-expression is used when the result of a non-trivial computation should be used multiple times within an expression calculation. Let expressions take the following form:

let name1=value1, ... return expression

Names defined in the let section (such as name1 in the template above) assume the values assigned to them, and these names and their bound values are available for reference from within expression.

For example, let r=@webrequests.getDetails(username) return r.body.firstName ? r.body.firstName : r.body.alias would invoke the web-request function getDetails once, but will refer to the result several times throughout the computation.

Let-expressions may be nested. Defining or re-defining a variable in a let-expression is only in effect for the course of that let-expression's return expression evaluation (and not for the definition of subsequent variables in the let statement).

For example, in the expression let a=1,... return (let a=9 return a)+a, the returned value will be 10 as opposed to 18 (as would be the case if the second definition for a would remain in effect until the end of the evaluation of the entire expression). Similarly, the expression let a=1, ... return (let b=9 return a)+b will result in an error as b is not defined outside the scope of the second let-expression.

A recursive-let expression is a variant of the let expression, where each variable definition can be used by subsequent variable definitions, in addition to the return expression. The syntax for a recursive-let expression is as follows:

letr name1=value1, ... return expression

The following two expressions are equivalent:
letr var1=val1, var2=val2 return expression and let var1=val1 return let var2=val2 return expression