v2 / vlib / v / tests / generics / generics_struct_with_option_fn_test.v
14 lines · 12 sloc · 243 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1struct Cmp[K] {
2 cmp ?fn (K, K) bool
3}
4
5fn (c Cmp[K]) compare(a K, b K) bool {
6 cmp := c.cmp or { return a < b }
7 return cmp(a, b)
8}
9
10fn test_generic_struct_with_option_fn() {
11 c := Cmp[int]{}
12 print(c.compare(1, 2))
13 assert c.compare(1, 2)
14}
15