Method syntax & the dot operator
The dot works on every value, and does the obvious thing:
| Expression | What it does |
|---|---|
obj.method(args) | Calls the method of an object, with self = obj |
obj.field | Reads a field of an object |
dict.key | Reads a key of a dict (nil if missing) |
module.fn(args) | Calls a function of a module imported with IMPORT://x as module |
value.fn(args) | Anything else: calls the function fn(value, args) |
The last line is called uniform call syntax: x.f(a, b) is the same as f(x, a, b) whenever x has no method or key called f. It means every function — built-in or your own — can be used as a method, and calls can be chained from left to right instead of being nested:
// The same computation, twice:
print(join(map(filter(split("5,12,8,30", ","), s => num(s) > 6), s => "#" + s), " "))
print("5,12,8,30".split(",").filter(s => num(s) > 6).map(s => "#" + s).join(" "))
// #12 #8 #30
ton.function shout(s) { return s.upper() + "!" }
print("hello".shout()) // HELLO!
print([3, 1, 2].sort().reverse()) // [3, 2, 1]
print(" padded ".trim().padStart(10, "."))
.length is also available as a property on strings and arrays ("abc".length is 3).