forked from luna/jorts
27 lines
666 B
Text
27 lines
666 B
Text
|
import io
|
||
|
|
||
|
// takes a function that receives two ints, returns an int
|
||
|
// Func is the function type keyword, to not switch it with fn (which declares
|
||
|
// a function)
|
||
|
fn function_tester (Func function ([int, int] -> int)) -> int {
|
||
|
func(2, 2)
|
||
|
}
|
||
|
|
||
|
fn add(int a, int b) -> int {
|
||
|
a + b
|
||
|
}
|
||
|
|
||
|
fn main () {
|
||
|
// passes the function add to function_tester
|
||
|
res := function_tester(add)
|
||
|
|
||
|
// you can also create functions and put them in variables. not putting a
|
||
|
// function name on the fn block makes it return a Func instance to be put
|
||
|
// in a variable
|
||
|
anonymous := (fn () {})
|
||
|
|
||
|
// anonymous has type Func ([] -> void)
|
||
|
|
||
|
io.puts("res = {res}")
|
||
|
}
|