v2 / vlib / v / tests / options / option_generic_cast_none_test.v
27 lines · 23 sloc · 573 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1pub type MyFn[T] = fn (mut my_state T)
2
3fn 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
12pub struct State {
13mut:
14 x int
15}
16
17fn handler(mut my_state State) {
18 my_state.x += 1
19}
20
21fn 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