diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-08-18 21:19:22 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-08-18 21:19:22 -0400 |
| commit | 2846ead8b8c60d67152187abb0f05529d94a048d (patch) | |
| tree | 62c4ac609f3af8ad046cb766036eb537cc1f3d2b /api/pointers.md | |
| parent | 6f3b2c073a968e57d787849dce42ff1253ed0102 (diff) | |
More docs
Diffstat (limited to 'api/pointers.md')
| -rw-r--r-- | api/pointers.md | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/api/pointers.md b/api/pointers.md index 67043084..6fc29013 100644 --- a/api/pointers.md +++ b/api/pointers.md @@ -102,3 +102,24 @@ when optional is @ptr: else: say("Oh, it was null") ``` + +## Using Pointers + +For convenience, most operations that work on values can work with pointers to +values implicitly. For example, if you have a struct type with a `.foo` field, +you can use `ptr.foo` on a pointer to that struct type as well, without needing +to use `ptr[].foo`. The same is true for array accesses like `ptr[i]` and method +calls like `ptr:reversed()`. + +As a matter of convenience, local variables can also be automatically promoted +to stack references when invoking methods that require a stack reference as the +first argument. For example: + +```tomo +func swap_first_two(arr:&[Int]): + arr[1], arr[2] = arr[2], arr[1] +... +my_arr := [10, 20, 30] // not a pointer +swap_first_two(my_arr) // ok, automatically converted to &my_arr +my_arr:shuffle() // ok, automatically converted to &my_arr +``` |
