v2 / vlib / v / tests / structs / struct_anon_generic_fn_field_test.v
37 lines · 31 sloc · 610 bytes · edf6c22e9c5110cfb1021a6d7959a7c2b2aa6b03
Raw
1pub struct ChunksArrayDecoder[T] {
2 callback fn (t T) = unsafe { nil }
3}
4
5pub fn new_chunks_array_decoder[T](callback fn (t T)) &ChunksArrayDecoder[T] {
6 return &ChunksArrayDecoder[T]{
7 callback: callback
8 }
9}
10
11struct Person {
12 name string
13 age int
14 kgs f32
15}
16
17@[heap]
18struct People {
19mut:
20 persons int
21 kgs f32
22}
23
24fn (mut people People) callback(person Person) {
25 people.persons++
26 people.kgs += person.kgs
27}
28
29fn module_callback(person Person) {
30 println('person: ${person}')
31}
32
33fn test_main() {
34 mut people := &People{}
35 mut decoder := new_chunks_array_decoder[Person](people.callback)
36 _ = decoder
37}
38