Expressions

Operands denote the elementary values in an expression.

Primary expressions are the operands for unary and binary expressions. A primary expressions may be a literal, an identifier denoting a variable, or a parenthesized expression.

Literals

Literals construct a value.

  1. | float_lit
  2. | string_lit
  3. | regex_lit
  4. | duration_lit
  5. | date_time_lit
  6. | RecordLiteral
  7. | ArrayLiteral
  8. | FunctionLiteral .

Record literals construct a value with the record type.

  1. RecordLiteral = "{" RecordBody "}" .
  2. RecordBody = WithProperties | PropertyList .
  3. WithProperties = identifier "with" PropertyList .
  4. PropertyList = [ Property { "," Property } ] .
  5. Property = identifier [ ":" Expression ]
  6. | string_lit ":" Expression .

Examples

  1. {a: 1, b: 2, c: 3}
  2. {a, b, c}
  3. {o with x: 5, y: 5}
  4. {o with a, b}

Array literals construct a value with the array type.

  1. ArrayLiteral = "[" ExpressionList "]" .
  2. ExpressionList = [ Expression { "," Expression } ] .

A function literal defines a new function with a body and parameters. The function body may be a block or a single expression. The function body must have a return statement if it is an explicit block, otherwise the expression is the return value.

  1. FunctionLiteral = FunctionParameters "=>" FunctionBody .
  2. FunctionParameters = "(" [ ParameterList [ "," ] ] ")" .
  3. ParameterList = Parameter { "," Parameter } .
  4. Parameter = identifier [ "=" Expression ] .
  5. FunctionBody = Expression | Block .
Examples of function literals
  1. add = (a,b) => a + b
  2. mul = (a,b) => a * b

Function literals are closures and may refer to variables defined in a surrounding block. Those variables are shared between the function literal and the surrounding block.

A call expression invokes a function with the provided arguments. Arguments must be specified using the argument name. Positional arguments are not supported. Argument order does not matter. When an argument has a default value, it is not required to be specified.

    Examples of call expressions
    1. f(a:1, b:9.6)
    2. float(v:1)

    Use short notation in a call expression when the name of every argument matches the name of every parameter.

    Examples of short notation in call expressions
    1. add(a: a, b: b) //long notation
    2. add(a, b) // short notation equivalent
    3. add = (a,b) => a + b
    4. a = 1
    5. b = 2
    6. // Don't mix short and long notation.
    7. add(a: a, b)
    8. add(a, b: b)

    Pipe expressions

    A pipe expression is a call expression with an implicit piped argument. Pipe expressions simplify creating long nested call chains.

    Pipe expressions pass the result of the left hand expression as the pipe argument to the right hand call expression. Function literals specify which if any argument is the pipe argument using the pipe literal as the argument’s default value. It is an error to use a pipe expression if the function does not declare a pipe argument.

    1. pipe_receive_lit = "<-" .
    Examples of pipe expressions

    Index expressions access a value from an array based on a numeric index.

    1. IndexExpression = "[" Expression "]" .

    Member expressions

    Member expressions access a property of a record. They are specified using an expression in one of the following forms:

    1. rec.k
    2. // or
    3. rec["k"]

    If rec contains an entry with property k, both rec.k and rec["k"] return the value associated with k. If rec does not contain an entry with property k, both rec.k and rec["k"] return null.

    1. MemberExpression = DotExpression | MemberBracketExpression .
    2. DotExpression = "." identifier .

    Conditional expressions evaluate a boolean-valued condition. If the result is true, the expression that follows the then keyword is evaluated and returned. If the result is false, the expression that follows the else keyword is evaluated and returned. In either case, only the branch taken is evaluated and only side effects associated this branch will occur.

    1. ConditionalExpression = "if" Expression "then" Expression "else" Expression .
    Conditional expression example
    1. color = if code == 0 then "green" else if code == 1 then "yellow" else "red"

    According to the definition above, if a condition evaluates to a null or unknown value, the else branch is evaluated.

    Operators

    Operators combine operands into expressions. The precedence of the operators is given in the table below. Operators with a lower number have higher precedence.

    The operator precedence is encoded directly into the grammar as the following.

    Dividing by 0 or using the mod operator with a divisor of 0 will result in an error.

    Also see .