Constructors create complex values from expressions and structuring information. Constructors can be used to create strings based on templates and values (interpolated strings), objects based on keys and values, and arrays.
Interpolated string constructors create values of type string by embedding the results of expressions within a containing string. Interpolated strings are denoted by backticks (`).
For example, the interpolated string `One plus one is ${1+1}` will compute the value of the expression 1+1, and embed the result in the containing string to yield One plus one is 2.
Within intepolated strings, the following elements may appear:
| Syntax | Description | Example |
|---|---|---|
${expression} | Expression to include in the string. It will be replaced by the expression's result coerced to a string | `Hello, ${user}` |
\` | Backtick | `\`` |
\$ | Dollar-sign | `\$500` |
\\ | Backslash | `here is a backslash: \\` |
| Other characters | Include character in resulting string | `this is a simple string` |
Object constructors are used to create values of type object. They consist of a sequence of key-value pairs. Their syntax is similar to the JavaScript syntax for objects. For example:
{
"key1" : value1,
key2 : value2,
...
}Objects may be empty (i.e., not contain any key-value pairs). Keys can be denoted as string literals, or valid identifiers (i.e., begin with a letter and consist only of letters and numbers). Values may be any expression.
To avoid ambiguity and errors, it is recommended to always put keys as string literals with "". In the example above, key1 is explicitly denoted as string literal while for key2 if such a variable exists, its value will be used; otherwise it will be coerced to a string literal as well.
The following example demonstrates the different notations for keys in an object:
{
identifierKey : 3,
"key with illegal identifier characters": 4
}Array constructors are used to create values of type array. They consist of a sequence of values, but may also be empty. Their syntax is similar to the JavaScript syntax for arrays:
[ value1, value2, ... ]To construct a value of type lambda, use the following syntax:
(param1, param2, ...) => bodywhere param1, param2,... is a set of comma-delimited valid identifiers for arguments, and body is any expression. The body expression may refer to all variables defined in the scope of the lambda construction as well as the arguments. See Lambda Invocations.
Since constructors use expressions to denote values, constructs can be nested and composed to create complex structures. For example, one can create an object with an interpolated string and an array as two attributes using the following expression:
{
interpolatedString: `1 plus 1 are ${1+1}`,
array: [ 2, 3, 1+2 ]
}A lambda value cannot be contained within arrays and objects.