v2 / vlib / v / tests / generics / generics_with_variadic_generic_args_test.v
31 lines · 26 sloc · 606 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1struct Node[T] {
2mut:
3 data T
4 next &Node[T] = unsafe { nil }
5}
6
7struct SinglyLinkedList[T] {
8mut:
9 first_node &Node[T] = unsafe { nil }
10}
11
12fn init_singlylinkedlist[T](nodes ...Node[T]) SinglyLinkedList[T] {
13 mut current_node := &nodes[0]
14
15 for i in 0 .. nodes.len - 1 {
16 current_node = &nodes[i]
17 current_node.next = &nodes[i + 1]
18 }
19
20 return SinglyLinkedList[T]{&nodes[0]}
21}
22
23fn test_generic_with_variadic_generic_args() {
24 sll := init_singlylinkedlist[int](Node[int]{ data: 1 }, Node[int]{
25 data: 2
26 }, Node[int]{
27 data: 798
28 })
29 println(sll.first_node.next)
30 assert sll.first_node.next.data == 2
31}
32