Skip to content

Commit 6595fb6

Browse files
committed
lots of improvements that where noted on discord
1 parent 67f40ea commit 6595fb6

1 file changed

Lines changed: 37 additions & 34 deletions

File tree

content/posts/odin-guide.md

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ This is a table that hold a comparison of all fundamental types you should know
3636
| type identifier | `typeid` | `Type` |
3737
| null | `nil` | `null` |
3838

39+
This table doesn't show all of odin's types, odin has many build in types compared to other languages,
40+
but all the important types are in this type table. Some examples of types that arent in the table are:
41+
`b8`, `b16`, `b32`, `b64`, `string16`, `cstring16`, `rawptr`, `complex32`, `complex64`,
42+
`complex128`, `quaternion64`, `quaternion128`, `quaternion256`, `matrix`, `map`, `array`.
43+
But to be fair, you do not need to know what these types all are, i just mentioned them so that you know they exist.
44+
3945
In odin types are first class, meaning types are values.
4046
This means functions are values and can be assigned like any other value.
4147
All types like `int` and `float` are of type `typeid`.
@@ -96,8 +102,8 @@ proc() -> (int, int) // two return values
96102
proc() -> (a: int, b: int) // two named return values
97103
proc() -> (a, b: int) // equivelant to above
98104
proc() -> (a := 10) // default value for return value
99-
proc() -> (..int) // variable return value amount
100-
proc() -> (nums: ..int) // named variable return value amount
105+
proc() -> (..int) // error (odin doesnt support variadic returns)
106+
proc() -> (nums: ..int) // error (odin doesnt support variadic returns)
101107
```
102108

103109
usage of default argument values:
@@ -109,7 +115,9 @@ b = foo(4) // overrides default value
109115
b = foo(x=4) // overrides default value by name
110116
```
111117

112-
usage of varying number of arguments:
118+
usage of varying number of arguments (variadic parameters):
119+
120+
> variadic parameters are just syntactic sugar for slices `..int` and `[]int` are identical in structure
113121
114122
```odin
115123
sum :: proc(nums: ..int) -> int {
@@ -131,15 +139,10 @@ foo :: proc() -> int {
131139
return a
132140
}
133141
134-
// with named output
135-
foo :: proc() -> (a: int) {
136-
a = 10
137-
}
138-
139142
// with named output and naked return statement
140143
foo :: proc() -> (a: int) {
141144
a = 10
142-
return // a return without arguments returns the named return value
145+
return // return still required (returns the named return value)
143146
}
144147
```
145148

@@ -216,7 +219,7 @@ for &value in some_array {
216219
value = something // element can be modified
217220
}
218221
219-
// map loop by referende (key can not be referenced)
222+
// map loop by reference (key can not be referenced)
220223
for key, &value in some_map {
221224
value += 1
222225
}
@@ -285,7 +288,7 @@ delete(my_map)
285288

286289
## Pointers
287290

288-
pointers have the same semantics as in c, but not the same syntax. using `^type` as pointer types, `ptr^` as dereference syntax, and `&value` as the adress of operator.
291+
pointers have the same semantics as in c, but not the same syntax. using `^type` as pointer types, `ptr^` as dereference syntax, and `&value` as the address-of operator.
289292

290293
```c
291294
// c
@@ -301,12 +304,13 @@ p: ^int = &x
301304
p^ = 2
302305
```
303306

304-
There is no such thing as pointer aritmatic like in c,
305-
because unlike in c, arrays are not just pointers,
306-
for pointer arritmatic like behaviour there are "multi pointers" of the `[^]T` type,
307+
There is no such thing as pointer arithmetic like in c,
308+
because unlike in c, arrays are not just fancy pointers, but actual value types,
309+
for pointer arithmetic like behaviour there are "multi pointers" of the `[^]T` type,
307310
which are pointers that map to multiple items, and can be indexed like an array.
308-
multi pointers are easiest to use with the `raw_data()` buildin call.
311+
multi pointers are easiest to use with the `raw_data()` builtin call.
309312
the `raw_data` is a builtin which returns the underlying data of a builtin data type as a multi pointer.
313+
the builtin `make()` procedure can also return multi pointers.
310314

311315
simple usage example of a multi pointer:
312316

@@ -340,12 +344,12 @@ What multi pointers do not support:
340344

341345
Instead of headers of modules or namespaces, the Odin language uses packages.
342346
Packages in odin are directory based, similar to how golang manages packages,
343-
this makes using submodules usefull, and makes it so you dont need a package manager.
347+
this makes using submodules useful, and makes it so you dont need a package manager.
344348
In Odin a package is a directory of Odin code files, all of which have the same package declaration at the top.
345349
Make a file part of a package by putting the `package package_name` declaration at the top of the odin files in the package.
346350
A directory cannot contain more than 1 package, so you can not have different package declarations in the same directory.
347351
To import a package (make it accesable), you use the `import` keyword.
348-
To import a standard library package you can use a prefex like `import "core:fmt"` where `core:` is the library prefix.
352+
To import a standard library package you can use a prefix like `import "core:fmt"` where `core:` is the collection prefix.
349353
If no prefix is specified the package will be searched relative to the current file path.
350354
Packages can be namespaced by using the `import foo "core:fmt"` syntax.
351355

@@ -357,7 +361,7 @@ Packages can be namespaced by using the `import foo "core:fmt"` syntax.
357361

358362
## Casting
359363

360-
In odin there is no implicit widening type conversion like in c, all types must be manually cast, luckily odin has a nice and simple syntax for type conversions and casting:
364+
In odin there is no implicit widening type conversion like in c, and only a [very short list of implicit conversions](https://odin-lang.org/docs/overview/#implicit-type-conversions) nearly all types must be manually cast, luckily odin has a nice and simple syntax for type conversions and casting:
361365

362366
The expression `T(value)` converts `value` to the `T` type:
363367
```odin
@@ -378,14 +382,11 @@ i: i64 = 123
378382
f: f64 = transmute(f64)i
379383
```
380384

381-
## Function Overloading
385+
## Prodecure (Function) Overloading
382386

383-
Unlike in many other languages, operator overloading in Odin is explicit,
384-
the reason being that procedures can be nested within procedures and, as a result,
385-
determining which procedure should be used in the case of implicit overloading is complex,
386-
therefore explicit overloading makes more sense.
387+
Unlike in many other languages, procedure overloading in Odin is explicit.
387388

388-
Here is an example of how to do function overloading:
389+
Here is an example of how to do procedure overloading:
389390

390391
```odin
391392
bool_to_string :: proc(b: bool) -> string {
@@ -402,7 +403,7 @@ to_string :: proc{bool_to_string, int_to_string}
402403

403404
## Static Arrays / Slices / Dynamic Arrays
404405

405-
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.
406+
In odin a static array is typed like `[N]T`, or `[?]T` for inferred size (only in a literal), and a slice like `[]T`, and a dynamic array like `[dynamic]T`, or `[dynamic;N]T` for a dynamic array with a fixed capacity.
406407

407408
An array in odin is just like a struct in that its value type that contains all of its own data.
408409

@@ -415,13 +416,12 @@ Slices and dynamic arrays are simple small structures with a pointer to an under
415416
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.
416417

417418
types of arrays:
418-
| Type: | Syntax |
419-
| --------------------------------- | -------------- |
420-
| static array | `[N]T` |
421-
| static array (inferred type) | `[?]T` |
422-
| slice | `[]T` |
423-
| dynamic array | `[dynamic]T` |
424-
| dynamic array (explicit capacity) | `[dynamic;N]T` |
419+
| Type: | Syntax |
420+
| ------------------------------ | -------------- |
421+
| static array | `[N]T` |
422+
| slice | `[]T` |
423+
| dynamic array | `[dynamic]T` |
424+
| dynamic array (fixed capacity) | `[dynamic;N]T` |
425425

426426
static arrays:
427427
```odin
@@ -434,7 +434,7 @@ value: int = array[0] // index
434434
array: [4]int = [4]int{1, 2, 3, 4} // explicit literal type
435435
array: [4]int = {1, 2, 3, 4} // inferred literal type
436436
array := [4]int{1, 2, 3, 4} // inferred type
437-
array := [?]int{1, 2, 3, 4} // inferred size
437+
array := [?]int{1, 2, 3, 4} // inferred literal size
438438
```
439439

440440
slices (array views):
@@ -461,6 +461,7 @@ dyn_array := make([dynamic]int, 0, 4) // initialize with make
461461
value: int = dyn_array[0] // index
462462
463463
// dynamic literals are not allowed because they hide allocations
464+
// but they can be enabled by using #+feature dynamic-literals
464465
dyn_array := [dynamic]int{1, 2, 3, 4} // error
465466
466467
// fixed capacity dynamic literals are however allowed
@@ -544,12 +545,14 @@ Build in fields like `xyzw` and `rgba` are available on any array with a length
544545
```odin
545546
Vector3 :: [3]f32
546547
foo :: proc(a: Vector3) -> f32 {
547-
return a.x + a.y + a.z // notice xyz is buildin
548+
return a.x + a.y + a.z // notice xyz is builtin
548549
}
549550
```
550551

551552
## Polymorphism (Generics)
552553

554+
The odin language specifically uses a form of generics called "Parametric Polymorphism"
555+
553556
## Strings
554557

555558
## Function Pointers / Function Types

0 commit comments

Comments
 (0)