diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-08-18 21:23:28 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-08-18 21:23:28 -0400 |
| commit | 3fbd242ea335b58e05cdce775d0d170384c84078 (patch) | |
| tree | 9cebe17fa481d01ca151f24917633fce8ed9d5be /api | |
| parent | 1fa1c063db27df66ece48cadd68c76eed716d161 (diff) | |
Update docs
Diffstat (limited to 'api')
| -rw-r--r-- | api/pointers.md | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/api/pointers.md b/api/pointers.md index 6fc29013..4258c329 100644 --- a/api/pointers.md +++ b/api/pointers.md @@ -11,19 +11,26 @@ don't need to save the pointer anywhere. Pointers are the way in Tomo that you can create mutable data. All datastructures are by default, immutable, but using pointers, you can create a region of memory where different immutable values can be held, which change -over time. +over time. Essentially, you can think about mutation as the act of creating +a new, different value and assigning it to a pointer's memory location to +replace the value that previously resided there. ```tomo func no_mutation_possible(nums:[Int]): - nums[1] = 10 // Type error! + nums[1] = 10 // This performs a copy-on-write and creates a new array +... +my_nums := [0, 1, 2] +no_mutation_possible(my_nums) +>> my_nums += [0, 1, 2] func do_mutation(nums:@[Int]): - nums[1] = 10 // okay - + nums[1] = 10 // The mutates the value at the given pointer's location ... - my_nums := @[0, 1, 2] do_mutation(my_nums) +>> my_nums += [10, 1, 2] ``` In general, heap pointers can be used as stack pointers if necessary, since |
