\{} interpol operator
\{ }, text interpolation operator
The text interpolation operator \{..}
inserts the value of an expression (converted as text
) into a text value. This operator also serves for text concatenation.
Examples
Simple insertion of variables:
a = 1
b = true
d = date(2024, 1, 31)
show scalar "" with "\{a}" // 1
show scalar "" with "\{b}" // true
show scalar "" with "\{d}" // 2024-01-31
show scalar "" with "\{a}\{b}" // 1true, concatenation.
Beware: do not forget the \
, it is \{ ..}
.
Insertion of complex expressions:
table T = with
[| as A, as B |]
[| 1, "a" |]
[| 2, "b" |]
[| 3, "c" |]
[| 4, "d" |]
[| 5, "e" |]
show scalar "" with "\{count(T.*)}" // 5
show scalar "" with "\{first(T.B) sort T.A}" // a
show scalar "" with "\{if true then 123 else 456}" // 123
show scalar "" with "\{1 + 2}" // 3
show scalar "" with "\{whichever(42 into T)}" // 42
show scalar "" with "\{100}" // 100
The following classes of complex expressions are supported, in the same order as in the above example:
- Arbitrary function calls, with or without options.
- Conditional expressions.
- Operators, such as
+
orinto
. - Literals.
For the sake of simplicity, we reject the following for text interpolation:
- The
[
and]
symbols, including lookups and multi-column options (e.g.,sort [T.A, T.B]
). - Nested text interpolation.
- Multi-line text interpolation.
All subexpressions in a complex expression must also adher to the same rules. For example:
// 3
show scalar "" with "\{if true or false then (1 + 2) else (3 + 4)}"
Although the outer expression is if-then-else, its conditional and both of the branches are themselves complex expressions. On the other hand, if we write the following:
table T[t] = with
[| as t, as N |]
[| 1, 5 |]
[| 2, 4 |]
[| 3, 3 |]
[| 4, 2 |]
[| 5, 1 |]
show scalar "" with "\{1 + T.N[1]}" \\ WRONG! ERRO: '[' is not allowed in text templates, please move complex elements to an intermediate variable.
it will fail to compile because T.N[1]
is not a valid expression for text interpolation:
'[' is not allowed in text templates, please move complex elements to an intermediate variable.
The solution would be to assign T.N[1]
to an intermediate variable:
table T[t] = with
[| as t, as N |]
[| 1, 5 |]
[| 2, 4 |]
[| 3, 3 |]
[| 4, 2 |]
[| 5, 1 |]
x = T.N[1]
show scalar "" with "\{1 + x}" // 6
Remarks
The number
values benefit from extra formatting options "\{myNumber:000.00}"
. The number format is specified by the token found after the semi-colon.
The date
values benefit from extra formatting options "\{myDate:yyyy-MM-dd}"
. The date format is specified by the token found after the semi-colon.
Interpolating numbers and dates with formatting options:
a = 12.3
d = date(2024, 1, 31)
show scalar "" with "\{a:000.00}" // 012.30
show scalar "" with "\{d:MMM dd, yyyy}" // Jan 31, 2024