-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add information about passing procedures as parameters #205
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -779,6 +779,29 @@ foo :: proc{ | |
} | ||
``` | ||
|
||
### Passing procedures as parameters | ||
|
||
Procedures are values, and can be passed into a callee as parameter like other values. | ||
```odin | ||
foo :: proc() -> int { return 2 } | ||
fmt.println(foo) | ||
``` | ||
Unlike in C or C++, procs are values, and can be passed by value. Passing a proc as a value is equivalent to passing the address of a function as a function pointer in C. | ||
|
||
When writing a procedure that expects a procedure value as a parameter, use the procedure's signature as the parameter's type: | ||
```odin | ||
my_map_reduce :: proc(mapper: proc(int) -> int, reduce: proc(int, int) -> int, values: []int, init := 0) -> (out: int) { | ||
out = init | ||
for v in values { | ||
out = reduce(out, mapper(v)) | ||
} | ||
return out | ||
} | ||
|
||
values := []int{1, 2, 3} | ||
mag_squared := my_map_reduce(proc(x: int) -> int{ return x*x }, proc(a, b: int) -> int{ return a+b }, values) | ||
fmt.println(mag_squared) | ||
``` | ||
|
||
## Basic types | ||
Odin's basic types are: | ||
|
@@ -1802,6 +1825,18 @@ x := p^ // ^ on the right | |
|
||
**Note:** Unlike C, Odin has no pointer arithmetic. If you need a form of pointer arithmetic, please use the `ptr_offset` and `ptr_sub` procedures in the `"core:mem"` package. | ||
|
||
**Note:** Unlike function pointers in C, procedures are ordinary values, so you don't need a function pointer to have a mutable proc variable that can contain different procedures. | ||
Since a proc is an ordinary value, a pointer to a proc can be used to mutate a proc variable, and change what it is pointing to: | ||
```odin | ||
change_proc_ptr :: proc(p : ^proc(int)->int) { | ||
p^ = proc(x:int) -> int { return x + 1 } | ||
} | ||
Comment on lines
+1831
to
+1833
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is probably more confusing because people will scan this and think you are meant to pass a procedure by pointer, even if it is clear from context you shouldn't. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, agreed. 🤔 maybe a better example would be to have a procedure that returns one procedure or another based on parameter, and assign
? |
||
p : proc(int)->int = proc(x:int) -> int { return x } | ||
fmt.println(p(1)) // 1 | ||
change_proc_ptr(&p) | ||
fmt.println(p(1)) // 2 | ||
``` | ||
|
||
### Structs | ||
A `struct` is a record type in Odin. It is a collection of fields. Struct fields are accessed by using a dot: | ||
```odin | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have put this before "Basic types", which should be moved further ahead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 Yeah, i wanted it to be a subsection under
Procedures
, but Procedures comes before "Basic types". Do you have a recommendation for where it should go instead?