Fixing array examples
This commit is contained in:
27
examples/arrays_and_slices.kl
Normal file
27
examples/arrays_and_slices.kl
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
Static arrays are exactly like c arrays, difference is
|
||||
we can easily then get a slice of that array.
|
||||
|
||||
Slices are static arrays + length, they simplify array handling.
|
||||
This allows us to pass an array into a function easily, then that function
|
||||
can still access that length information easily.
|
||||
|
||||
Passing a pointer to array + length is a common pattern in C. So it would
|
||||
be nice if handling of that was simplified.
|
||||
*/
|
||||
|
||||
|
||||
main :: (): int
|
||||
static_array: [8]int
|
||||
|
||||
// We can get size of array using length_of builtin
|
||||
#assert(length_of(static_array) == 8)
|
||||
|
||||
element: int = static_array[0]
|
||||
assert(static_array[1] == 0)
|
||||
assert(element == 0)
|
||||
// Variables are zeroed by default
|
||||
// assert(static_array[7] == 0)
|
||||
|
||||
// for static_array
|
||||
// it = 1
|
||||
Reference in New Issue
Block a user