Pointers
Single-item
"Normal" pointers that point to one thing.
- Type
*Type- Make a pointer
&val- Dereference
ptr.*- To slice (of len 1)
ptr[0..1]- Difference
ptr1 - ptr2
Arrays
An array has the type syntax [len]Type, so a single-item pointer to an array, or an array pointer has type *[len]Type. They support a few convenience features so you don’t have to deref every time.
- Type
*[len]Type- Index
ptr[idx]- To slice
ptr[start..end]orptr[start..]- Length
ptr.len- Subtraction
ptr1 - ptr2- To/from int
@ptrFromInt(int),@intFromPtr(ptr)
Many-item
These are like normal pointers but semantically it points to the first element in a sequence, like in C. You cannot directly dereference a many-item pointer. The type syntax is very similar to that of arrays. They do not store any length information.
- Type
[*]Type(the*is literal), or with a sentinel value:[*:sentinel]Type- Index
ptr[idx]- To slice
ptr[start..end]- Arithmetic
ptr + int(orptr[int..]),ptr - int- Subtraction
ptr - ptr
Slices
These are fat pointers, which store the actual pointer (a many-item pointer), and the length of what it points to. They are similar to arrays, but the length of an array is known at comptime, while that of the slice is known at runtime.
Slices can be downgraded to a many-item pointer using type coercion. Also, pointer arithmetic should not be used with slices. Instead, use a many-item pointer.
Slice to array upgrading If the length and slicing indexes are known at comptime, slices can get automatically upgraded to an array pointer. For example: |
- Type
[]Type, or with a sentinel value:[:sentinel]Type- Index
slc[idx]- To slice
slc[start..end], orslc[start..]- Length
slc.len- Pointer
slc.ptr(mutable, but should not be mutated)
Any "to slice" operation can have a sentinel value by using val[start..end :sentinel].