Statements and control flow#
Statements appear at the top level, in function bodies, and inside blocks. Semicolons are accepted, and the grammar can infer some statement terminators before block closes or new statement starters.
Variables#
let total: decimal = 0;
var count = 1, label: string = "open";
const enabled = true;Destructuring assignments are statements:
{ name: vendor_name, code } = vendor;
[first, ...rest, _] = values;Blocks#
{
let scoped = true;
return scoped;
}Conditionals#
if (enabled) {
return total;
} else {
return 0;
}
if enabled return total;if conditions may be parenthesized or bare expressions.
Loops#
while (count < 10) {
count += 1;
}
for (let i = 0; i < 10; i++) {
total += i;
}
for (item in items) {
total += item.amount;
}
for item in items {
total += item.amount;
}while conditions are parenthesized. for in loops may be parenthesized or bare, and the loop binding may include var, let, or const.
Switch, match, and when statements#
switch (status) {
case "open":
return 1;
default:
return 0;
}
match value {
case 1:
return "one";
default:
return "other";
}
when {
amount > 100 -> "large",
else -> "normal",
}switch and match statement cases use case value: and default: blocks. when statement cases use the same arrow case syntax as when expressions and are terminated as a statement.
Returns and loop control#
return value;
return;
break;
continue;Locking#
lock posting_batch;
unlock posting_batch;Use explicit locking only around state that must be protected by the runtime.
Try, catch, finally, and attempt#
try {
post_batch(batch);
} catch (err: PostingError) {
raise posting_failed(batch.id) with {
batch_id: batch.id;
};
} finally {
unlock batch;
}
attempt {
post_batch(batch);
}try accepts zero or more catch clauses and an optional finally block. catch parameters must name a type.
Throw and raise#
throw error_value;
raise message posting_policy_required(company_code_param) with {
company_code: company_code_param;
};
throw message posting_failed(batch_id);
raise posting_policy_required(company_code_param);User-facing failures should use declared messages.
Query statements#
Query statements are allowed inside function and control-flow blocks.
select vendor_id, vendor_name from Vendors where active = true;
delete from Vendors where active = false;DDL statements#
The grammar includes a small DDL subset:
create table Vendors;
delete table Vendors;
add field email to table Vendors;
delete field email from table Vendors;