Operators
| Category | Operators |
|---|---|
| Arithmetic | + - * / % |
| Comparison | == != < <= > >= |
| Logical | &&/and, ||/or, ! |
| Nil-coalescing | ?? |
| Membership | in |
| Bitwise | & | ^ ~ << >> |
| Assignment | = += -= *= /= %= &= |= ^= <<= >>= |
| Increment/decrement | ++ -- (postfix) |
| Ternary | cond ? a : b |
Notes:
+on two numbers adds them; if either side is a string orton.html, it concatenates (converting the other side to text first) — the result iston.htmlif either operand was.<,<=,>,>=compare two numbers numerically, or two strings/ton.htmlvalues 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]istrue./and%both raise "Division by zero" for a zero right-hand side, rather than producinginf/NaNsilently.and/orare exact synonyms for&&/||— use whichever reads better.??("nil-coalescing") evaluates its left side and returns it as-is unless it'snil, 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".intests membership:value in arraychecks the array's elements (using the same equality rules as==),value in dictchecks the dict's keys, andvalue in stringchecks 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, so6 & 3is 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:
| Operator | Meaning | Example |
|---|---|---|
** / **= | Power (right-associative) | 2 ** 10 is 1024 |
.. / ..= | Range, end excluded / included | 0..5, 1..=10 |
|> | Pipe: the left value becomes the first argument | x |> f(y) is f(x, y) |
?. | Optional chaining: nil instead of an error | user?.address?.city |
??= | Assign only if currently nil | config.port ??= 8080 |
. | Property / method access | point.x, "hi".upper() |
=> | Arrow function | n => n * 2 |
-2 ** 2is-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" * 3is"ababab").a..bevaluates to an array of the integers fromaup tob - 1(a..=bincludesb); in aforloop it counts lazily, sofor i in 0..1000000never builds an array.?.works on property access, indexing and calls:obj?.name,list?.[0],callback?.(). Use it at each step that may benil.