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