v2 / vlib / v / tests / fns / fn_generic_resolve_test.v
32 lines · 24 sloc · 598 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1pub type MyFn[T] = fn (mut my_state T)
2
3fn 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
12pub struct Stateless {}
13
14pub fn higher_order_fn_stateless(callback MyFn[Stateless]) {
15 initial_state := Stateless{}
16 higher_order_fn[Stateless](initial_state, callback)
17}
18
19pub struct State {
20mut:
21 x int
22}
23
24fn handler(mut my_state State) {
25 my_state.x += 1
26}
27
28fn test_main() {
29 initial_state := State{}
30 new_state := higher_order_fn[State](initial_state, handler)
31 assert new_state.x == 2
32}
33