Add information about passing procedures as parameters#205
Add information about passing procedures as parameters#205NHDaly wants to merge 2 commits intoodin-lang:masterfrom
Conversation
I couldn't find this when i first read the documentation, and I came away assuming that Odin _didn't support_ passing (pointers to) functions like you could do in C. Of course, once I got more familiar I realized that _procs are just values_ so they can be passed like any ordinary value. I wanted to spare a future reader that confusion, by making this more explicit. Please feel free to edit/adjust any of the language here; it's my first time contributing to the Odin project. Thank you!
| } | ||
| ``` | ||
|
|
||
| ### Passing procedures as parameters |
There was a problem hiding this comment.
You have put this before "Basic types", which should be moved further ahead.
There was a problem hiding this comment.
🤔 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?
| change_proc_ptr :: proc(p : ^proc(int)->int) { | ||
| p^ = proc(x:int) -> int { return x + 1 } | ||
| } |
There was a problem hiding this comment.
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.
Yeah, agreed. 🤔 maybe a better example would be to have a procedure that returns one procedure or another based on parameter, and assign p to each of those at the call site?
Something like
select_proc :: proc(s : int) -> proc(int)->int {
switch {
case n == 0:
return proc(x:int) -> int { return x }
case n == 1:
return proc(x:int) -> int { return x + 1 }
}
}
p := select_proc(0)
fmt.println(p(1)) // 1
p := select_proc(1)
fmt.println(p(1)) // 2
?
|
Closing because the feedback hasn't been addressed. |
|
Oops, thanks Laytan - sorry for my delay; this fell through the cracks. I'll respond to the feedback now |
|
If you're able to reopen, i'll try to address the feedback. Thanks for the review and the ping |
|
@laytan - would you be up to reviewing this? I'm going through and trying to clean up some of my pending PRs. :) |
I couldn't find this when i first read the documentation, and I came away assuming that Odin didn't support passing (pointers to) functions like you could do in C.
Of course, once I got more familiar I realized that procs are just values so they can be passed like any ordinary value. I wanted to spare a future reader that confusion, by making this more explicit.
Please feel free to edit/adjust any of the language here; it's my first time contributing to the Odin project. Thank you!