Skip to content

Commit 810274e

Browse files
committed
work on odin arrays section
1 parent d8d9afd commit 810274e

1 file changed

Lines changed: 86 additions & 7 deletions

File tree

content/posts/odin-guide.md

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ This means functions are values and can be assigned like any other value.
4141
All types like `int` and `float` are of type `typeid`.
4242

4343
```odin
44-
a :: int; // a is int
45-
b: a = 1; // b is now an int
44+
a :: int // a is int
45+
b: a = 1 // b is now an int
4646
```
4747

4848
In Odin `typeid` is a bit of an overloaded term, because there is a difference between a compile time typeid and a runtime typeid.
@@ -199,9 +199,9 @@ int* p = &x;
199199

200200
```odin
201201
// odin
202-
x := 1;
203-
p: ^int = &x;
204-
p^ = 2;
202+
x := 1
203+
p: ^int = &x
204+
p^ = 2
205205
```
206206

207207
There is no such thing as pointer aritmatic like in c,
@@ -303,9 +303,88 @@ int_to_string :: proc(i: int) -> string {
303303
to_string :: proc{bool_to_string, int_to_string}
304304
```
305305

306-
## Array Programming (Operator Overloading)
306+
## Static Arrays / Slices / Dynamic Arrays
307+
308+
In odin a static array is typed like `[N]T`, or `[?]T` for inferred size, and a slice like `[]T`, and a dynamic array like `[dynamic]T`, or `[dynamic;N]T` for a dynamic array with a fixed capacity.
309+
310+
An array in odin is just like a struct in that its value type that contains all of its own data.
311+
312+
A slice is just a view into an existing array, a slice is basically a pointer and length of an array. Any array can be turned into a slice using the `array[low:high]` syntax.
313+
314+
Dynamic arrays are similar to slices, but their lengths may change during runtime. Dynamic arrays are resizeable and they are allocated when needed using the current context allocator.
315+
316+
Slices and dynamic arrays are simple small structures with a pointer to an underlying array, therefore copying or passing a slice or dynamic array will not copy the underlying data. Because of this they work kinda like reference types.
317+
318+
The zero value of a slice is nil. A nil slice has a length of 0 and does not point to any underlying memory. Slices can be compared against nil and nothing else.
319+
320+
static arrays:
321+
```odin
322+
// basic usage
323+
array: [4]int // declare
324+
array: [4]int = [4]int{1, 2, 3, 4} // initialize with literal
325+
value: int = array[0] // index
326+
327+
// other ways of initializing
328+
array: [4]int = [4]int{1, 2, 3, 4} // basic array literall
329+
array: [4]int = {1, 2, 3, 4} // inferred literal type
330+
array := [4]int{1, 2, 3, 4} // inferred type
331+
array := [?]int{1, 2, 3, 4} // inferred size
332+
```
333+
334+
slices (array views):
335+
```odin
336+
// basic usage
337+
slice: []int // declare
338+
slice: []int = []int{1, 2, 3, 4} // initialize with literal
339+
slice: []int = array[:] // get slice from array
340+
value: int = slice[0] // index
341+
342+
// the following slicing operations are all equivelant (if array has a lenghth of 6)
343+
slice := array[0:6]
344+
slice := array[:6]
345+
slice := array[0:]
346+
slice := array[:]
347+
```
307348

308-
## Arrays / Slices
349+
dynamic arrays:
350+
```odin
351+
// basic usage
352+
dyn_array: [dynamic]int // declare
353+
dyn_array: [dynamic; 16]int // declare with max capacity
354+
dyn_array := make([dynamic]int, 0, 4) // initialize with make
355+
value: int = dyn_array[0] // index
356+
357+
// the following ways of initializing a dynamic array are not allowed (dynamic literals where removed)
358+
dyn_array: [dynamic]int = [dynamic]int{1, 2, 3, 4} // error
359+
dyn_array := [dynamic]int{1, 2, 3, 4} // error
360+
dyn_array: [dynamic]int = {1, 2, 3, 4} // error
361+
362+
// get length of dynamic array
363+
length: int = len(array)
364+
365+
// add to the dynamic array
366+
append(&array, 4)
367+
368+
// remove third element from the dynamic array
369+
ordered_remove(&array, 2)
370+
371+
// index the dynamic array
372+
value: int = array[0]
373+
374+
// clear the dynamic array
375+
clear(&array)
376+
```
377+
378+
multi dimensional arrays:
379+
```odin
380+
// creating a 2D static array
381+
// creating a 3D static array
382+
// initializing a 2D array
383+
// initializing a 2D array with inferred type
384+
// indexing a 2D array
385+
```
386+
387+
## Array Programming (Operator Overloading)
309388

310389
## Polymorphism (Generics)
311390

0 commit comments

Comments
 (0)