| 1 | pub type MyFn[T] = fn (mut my_state T) |
| 2 | |
| 3 | fn higher_order_fn[T](initial_state T, maybe_callback ?MyFn[T]) State { |
| 4 | mut my_state := initial_state |
| 5 | if callback := maybe_callback { |
| 6 | callback[T](mut my_state) |
| 7 | callback[T](mut my_state) |
| 8 | } |
| 9 | return my_state |
| 10 | } |
| 11 | |
| 12 | pub struct State { |
| 13 | mut: |
| 14 | x int |
| 15 | } |
| 16 | |
| 17 | fn handler(mut my_state State) { |
| 18 | my_state.x += 1 |
| 19 | } |
| 20 | |
| 21 | fn test_main() { |
| 22 | mut state := State{} |
| 23 | state = higher_order_fn[State](state, ?MyFn[State](handler)) |
| 24 | state = higher_order_fn[State](state, ?MyFn[State](none)) |
| 25 | state = higher_order_fn[State](state, handler) |
| 26 | assert state.x == 4 |
| 27 | } |
| 28 | |