Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

NHDaly
Copy link

@NHDaly NHDaly commented Apr 13, 2024

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!

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
Copy link
Member

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.

Copy link
Author

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?

Comment on lines +1831 to +1833
change_proc_ptr :: proc(p : ^proc(int)->int) {
p^ = proc(x:int) -> int { return x + 1 }
}
Copy link
Member

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.

Copy link
Author

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

?

@laytan
Copy link
Sponsor Collaborator

laytan commented Aug 14, 2024

Closing because the feedback hasn't been addressed.

@laytan laytan closed this Aug 14, 2024
@NHDaly
Copy link
Author

NHDaly commented Aug 17, 2024

Oops, thanks Laytan - sorry for my delay; this fell through the cracks. I'll respond to the feedback now

@NHDaly
Copy link
Author

NHDaly commented Aug 17, 2024

If you're able to reopen, i'll try to address the feedback. Thanks for the review and the ping

@Kelimion Kelimion reopened this Aug 17, 2024
@NHDaly NHDaly requested a review from gingerBill August 20, 2024 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants