Operators

CategoryOperators
Arithmetic+ - * / %
Comparison== != < <= > >=
Logical&&/and, ||/or, !
Nil-coalescing??
Membershipin
Bitwise& | ^ ~ << >>
Assignment= += -= *= /= %= &= |= ^= <<= >>=
Increment/decrement++ -- (postfix)
Ternarycond ? a : b

Notes:

  • + on two numbers adds them; if either side is a string or ton.html, it concatenates (converting the other side to text first) — the result is ton.html if either operand was.
  • <, <=, >, >= compare two numbers numerically, or two strings/ton.html values lexicographically; comparing any other combination (or an array, a dict, a bool) is a runtime error rather than a silently meaningless result.
  • ==/!= use structural equality: numbers/bools/nil compare by value, a string and an html value with the same text are equal, and arrays/dicts compare element-by-element (recursively) rather than by reference — [1, 2] == [1, 2] is true.
  • / and % both raise "Division by zero" for a zero right-hand side, rather than producing inf/NaN silently.
  • and/or are exact synonyms for &&/|| — use whichever reads better.
  • ?? ("nil-coalescing") evaluates its left side and returns it as-is unless it's nil, in which case it evaluates and returns the right side instead — the right side is never evaluated otherwise, so it's safe to put a fallback that has side effects. Handy with dict lookups: dict["missing"] ?? "default".
  • in tests membership: value in array checks the array's elements (using the same equality rules as ==), value in dict checks the dict's keys, and value in string checks for a substring. The right-hand side must be an array, dict, or string/ton.html — anything else is a runtime error.
  • &, |, ^, ~, <<, >> treat both sides as integers (truncating any fractional part), operate on a 64-bit two's-complement representation, then convert the result back to TON618's usual number type — there's no separate integer type, so 6 & 3 is just a number like any other (2). Both operands must be numbers; anything else is a runtime error. &/|/^ all share one precedence level (looser than ==, tighter than &&) — use parentheses when mixing them for clarity.

Added in beta-2.0.0:

OperatorMeaningExample
** / **=Power (right-associative)2 ** 10 is 1024
.. / ..=Range, end excluded / included0..5, 1..=10
|>Pipe: the left value becomes the first argumentx |> f(y) is f(x, y)
?.Optional chaining: nil instead of an erroruser?.address?.city
??=Assign only if currently nilconfig.port ??= 8080
.Property / method accesspoint.x, "hi".upper()
=>Arrow functionn => n * 2
  • -2 ** 2 is -4: ** binds tighter than the unary minus, as in maths and Python.
  • + also concatenates two arrays ([1, 2] + [3] is [1, 2, 3], a new array); * repeats a string ("ab" * 3 is "ababab").
  • a..b evaluates to an array of the integers from a up to b - 1 (a..=b includes b); in a for loop it counts lazily, so for i in 0..1000000 never builds an array.
  • ?. works on property access, indexing and calls: obj?.name, list?.[0], callback?.(). Use it at each step that may be nil.

Edit this page on GitHub