Functions and expressions
Functions
function isLate(moment: time): bool
return not moment.between(opensAt, closesAt)
end
- Parameter and return types are written. A function with a return type must
returnon every path; the compiler checks. - A function may read and write fields and memory,
goto, start and stop timers,logand userandom; it cannot wait. - Recursion is allowed; its depth is limited by the budget.
- Functions are declared at the script's level.
Statements
let total = business.visitors + 1 // type inferred, immutable
var count = 0 // mutable
if total > 100 and not hasPower
goto Closed
elseif total == 0
log("empty")
else
business.visitors = total
end
while count < 3
count += 1
end
for i in 1 to 10 // 1..10 inclusive
ring()
end
if count == 0 then return
let shade = if lit then onColor else offColor
if … then …is the one-line form;if a then b else cis also an expression.for i in a to bevaluates its bounds once, does not run whena > b, andicannot be changed in the body.break,continueandreturnwork as usual.- Assignment is
=,+=,-=,*=,/=. The target is a localvar,memoryor awritesfield.
Operators
From the highest precedence to the lowest:
| Operators | |
|---|---|
call, . | clock().between(a, b) |
as | speed as int |
unary - | -x |
* / % | |
+ - | x-1 is always a subtraction |
== != < <= > >= | comparisons do not chain |
not | not a == b means not (a == b) |
and | short-circuits |
or | short-circuits |
a.f(b) is the same as f(a, b): clock().between(opensAt, closesAt),
neonColor.scaled(0.5).
Operands and arguments are evaluated left to right; in a compound assignment the target first, then the right side.
log
log(...) (server) writes to Play's log with the world's time. {expression} inserts a value:
log("closing, {business.visitors} visitors today")
A transaction logs at most 20 lines of 400 characters; more is cut and marked. Logging does not change behaviour, and lines of a transaction that fails are kept, marked as failed.