6. Expressions
6.1 Evaluation order#
Operands, arguments and elements evaluate left to right, once. and and or short-circuit. A conditional expression evaluates only the arm it picks.
6.2 Arithmetic#
| Operator | On int | On float |
|---|---|---|
+ - * | checked: overflow traps | IEEE |
/ | a float quotient, as Python's | IEEE |
// | floor division; by zero traps | floor division |
% | remainder with the divisor's sign, as Python's; by zero traps | IEEE remainder |
** | checked power with an int exponent that is not negative | float power |
unary - | checked | IEEE |
int and float never mix in one operation: count * 1.5 is an error and is written float(count) * 1.5. There is no bit operation, shift, wrapping or saturating form; a program that needs them calls a Base package.
6.3 Comparison and logic#
== != < <= > >= produce bool by §4.4 and do not chain. and, or, not take bool only: an int, a str or an optional is never a condition by itself. x in collection is membership: an element of a list or set, a key of a map, a substring of a str.
6.4 Conditional expression#
a if condition else b, with both arms of one type.
6.5 Conversions#
| Call | Meaning |
|---|---|
int(f) | truncates a float toward zero; traps on NaN or out of range |
int(s) | parses a str as a decimal integer, a sign and digits with spaces around them; int! |
float(i) | the nearest float |
float(s) | parses a str as a decimal number, a sign, digits with an optional fraction and an optional exponent, spaces around them; float! |
str(x) | the display of any value with one (§10.5) |
bool(x) | only from str: "true" or "false", else fails |
There is no cast. A conversion is a call, and one that can fail says so.
6.6 Members, calls, indexing, slicing#
value.field, value.method(args), Type.function(args), callable(args), list[i], map[key], list[a..<b], text[a..<b]. Indexing a list with an int out of range traps; reading map[key] yields V?; a slice of a list or a str is a copy (§11). Ranges a..<b and a..=b are values of type range that iterate ints.
6.7 Precedence#
From tightest: member, call, index; unary -; **; * / // %; + -; ..< ..=; in, is, is not, comparisons; not; and; or; if-else; =>; assignment. not sits below the comparisons so that not a == b negates the comparison, as in Python.