code / tomo-koans

Lines447 Tomo432 INI9 Markdown6
(38 lines)
1 # Heap Allocation with `@`
3 func main()
5 # By default, values in Tomo are immutable.
6 # To allow mutation, you need to allocate memory using `@`
8 nums := @[10, 20, 30]
9 nums[1] = 99
11 assert nums[1] == ???
13 nums.insert(40)
14 assert nums[4] == ???
16 # Allocated memory is not equal to other allocated memory:
17 a := @[10, 20, 30]
18 b := @[10, 20, 30]
19 pointers_are_equal := (a == b)
20 assert pointers_are_equal == ???
22 # The `[]` operator can be used to access the value stored
23 # at a memory location:
24 contents_are_equal := (a[] == b[])
25 assert contents_are_equal == ???
27 # Tables also require `@` to allow modifications:
28 scores := @{"Alice": 100, "Bob": 200}
30 scores["Charlie"] = 300
32 assert scores["Charlie"] == ???
34 # Without `@`, attempting to mutate will cause an error:
35 frozen := {"key": "value"}
36 frozen["key"] = "new value" # <- This will break, comment it out
38 assert frozen["key"] == "value"