The expression language
Grammar, operator precedence, how values convert between types, and what happens when an expression is wrong or too slow.
On this page
Formulas covers what expressions are for. This page is the exact behaviour: what the parser accepts, and what the evaluator does with it.
Literals#
| Form | Example |
|---|---|
| Number | 42, 0.5, .5 |
| Text | 'hello' or "hello", both quote styles work |
| Yes/no | true, false |
| List | [1, 1.05, 1], only animate() takes one |
Text inside an expression must be quoted. $dark_mode ? white : black$ reads white and black as variables, finds neither, and produces nothing; $dark_mode ? 'white' : 'black'$ is what was meant. This is the single most common mistake in a colour field.
Writing a literal dollar sign#
A $ opens an expression only when a second one closes it later in the same field. A lone $ is ordinary text, so Total: $5 is safe. Two of them are not: $5 or $10 looks like the expression 5 or and produces something unintended. Where a field may contain two dollar signs, put the text in an expression instead: $'$5 or $10'$.
Operators, weakest first#
Each row binds more tightly than the one above it.
| Level | Operators |
|---|---|
| 1 | a ? b : c |
| 2 | ?? |
| 3 | || |
| 4 | && |
| 5 | == != < <= > >= |
| 6 | + - |
| 7 | * / |
| 8 | unary -, ! |
So a || b && c means a || (b && c), and x + 1 > y means (x + 1) > y. Parentheses group anything.
What ?? actually treats as missing#
Only two things: empty text and a number that is not a number. Zero is not missing, and false is not missing.
'' ?? 'fallback' → 'fallback'
0 ?? 'fallback' → 0
false ?? 'fallback' → falseThat is what makes it right for optional data (a feed entry with no picture gives '', a weather provider with no humidity gives a non-number) and wrong as a general default. For "use 10 when the value is zero", write the condition out: $value == 0 ? 10 : value$.
Type conversion#
Values convert on use, and the rules are worth knowing because they decide what a comparison means.
To a number. true is 1 and false is 0. Text is parsed if it looks like a number, and is 0 otherwise, including text that is merely misspelled. Anything else is 0.
To yes/no. A number is true when it is not zero. Text is true when it is not empty, not 'false' and not '0', so the string 'no' is true, and so is 'off'. When a variable holds a word rather than a flag, compare it: $mode == 'on'$.
To text. A whole number loses its decimal part, so 1.0 prints as 1 and not 1.0. That is why a clock needs format(time_minute, '00') rather than plain concatenation.
When an expression is wrong#
A formula that does not parse does not show an error. It evaluates to 0, which reads as 0, as false, or as an empty position depending on the field. A layout that silently sits at the top left corner is usually a typo above it.
One place behaves differently on purpose: a filterFormula that does not parse lets entries through rather than hiding them. A broken filter that showed nothing would look exactly like an empty list, and a page that shows too much is a bug you can see.
The editor validates as you type, which is where an error is meant to be caught. The The limits on length, nesting and token count are checked there and again on upload.
When an expression is too slow#
Every expression is timed. One evaluation gets 2 ms; after three consecutive overruns the expression stops being evaluated for two seconds and keeps returning its last good value, then tries again.
Three consequences worth designing around:
- A theme cannot stall the launcher, however extravagant the arithmetic. It can only make its own value go stale.
- The symptom is a value that freezes, not one that disappears. An element stuck at a stale position, recovering every couple of seconds, is an expression over budget, not a layout bug.
- The budget belongs to the formula text, not to the element. Fifty repeater cards sharing one expression share its guard, and in the pause they all show the same last value. That is the case the limit exists for.
Output is capped at 10 000 characters, so an expression cannot inflate a string until the text layout chokes on it.
Where each variable means something#
Most variables are available everywhere. These are not:
| Variables | Valid inside |
|---|---|
app_name, app_package, app_profile, app_badge | An app field: a slot of an app_grid, or an app_launcher. |
item_* | A repeater card, including its rowHeight and filterFormula. |
scrub_pos, scrub_press, scrub_vis, scrub_over | A repeater with scrubVariable set. |
page_position, page_index, page_count, page_row | Relative to the page the element sits on. |
page_row_position | Like page_row, but it moves along with a vertical page change instead of snapping at the end. Use it when something should travel with the change. |
page_scroll_fraction, page_swipe_progress | Relative to the page swipe on the home screen. |
Outside its context a variable is simply unknown, which is 0 or empty text, not an error. A $app_name$ on a page background prints nothing at all.
Documentation for launcher 0.1.0-alpha Android 12 and later last updated