-
Notifications
You must be signed in to change notification settings - Fork 84
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?
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!
@@ -779,6 +779,29 @@ foo :: proc{ | |||
} | |||
``` | |||
|
|||
### Passing procedures as parameters |
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?
change_proc_ptr :: proc(p : ^proc(int)->int) { | ||
p^ = proc(x:int) -> int { return x + 1 } | ||
} |
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.
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 comment
The 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
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 |
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!