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
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions content/docs/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?


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_mapreduce :: proc(mapper: proc(int)->int, reduce: proc(int,int)->int, values: []int, init := 0) -> int {
NHDaly marked this conversation as resolved.
Show resolved Hide resolved
out := init
for v in values {
out = reduce(out, mapper(v))
}
return out
}

values := []int{1, 2, 3}
mag_sqrd := my_mapreduce(proc(x:int)->int{return x*x}, proc(a,b:int)->int{return a+b}, values)
NHDaly marked this conversation as resolved.
Show resolved Hide resolved
fmt.println(mag_sqrd)
```

## Basic types
Odin's basic types are:
Expand Down Expand Up @@ -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
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

?

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
Expand Down