aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/vlib/v/tests/generics_with_anon_generics_fn_test.v
diff options
context:
space:
mode:
Diffstat (limited to 'v_windows/v/vlib/v/tests/generics_with_anon_generics_fn_test.v')
-rw-r--r--v_windows/v/vlib/v/tests/generics_with_anon_generics_fn_test.v57
1 files changed, 57 insertions, 0 deletions
diff --git a/v_windows/v/vlib/v/tests/generics_with_anon_generics_fn_test.v b/v_windows/v/vlib/v/tests/generics_with_anon_generics_fn_test.v
new file mode 100644
index 0000000..b5bde8f
--- /dev/null
+++ b/v_windows/v/vlib/v/tests/generics_with_anon_generics_fn_test.v
@@ -0,0 +1,57 @@
+struct MyStruct<T> {
+ arr []T
+}
+
+fn (mut s MyStruct<T>) get_data(pos int) T {
+ return s.arr[pos]
+}
+
+fn (mut s MyStruct<T>) iterate(handler fn (T) int) int {
+ mut sum := 0
+ mut i := 0
+ for {
+ k := s.get_data<T>(i)
+ sum += handler(k)
+ i++
+ if i > 4 {
+ break
+ }
+ }
+ return sum
+}
+
+pub fn consume(data int) int {
+ return data
+}
+
+pub fn consume_str(data string) string {
+ return data
+}
+
+fn call<T>(f fn (T) T, v T) T {
+ return f(v)
+}
+
+struct Pair<T, U> {
+ a T
+ b U
+}
+
+fn test_generics_with_anon_generics_fn() {
+ mut s := MyStruct<int>{
+ arr: [1, 2, 3, 4, 5]
+ }
+ y := s.iterate<int>(consume)
+ println(y)
+ assert y == 15
+
+ assert call<int>(consume, 1) == 1
+ assert call<string>(consume_str, '1') == '1'
+ assert call(consume, 1) == 1
+ assert call(consume_str, '1') == '1'
+
+ pair := Pair<int, string>{1, 's'}
+ assert call(fn (v Pair<int, string>) Pair<int, string> {
+ return v
+ }, pair) == pair
+}