v2 / vlib / v / tests / generics / generic_function_argument_inference_test.v
26 lines · 21 sloc · 499 bytes · f1ee2cb2900d27fb376831e11c8d33ccd97fb604
Raw
1import math
2
3struct Vec5 {
4 x f64
5 y f64
6 z f64
7 a f64
8 b f64
9}
10
11fn vec5_from(value f64) Vec5 {
12 return Vec5{value, value, value, value, value}
13}
14
15fn (v Vec5) abs_oldstyle() Vec5 {
16 return Vec5{math.abs(v.x), math.abs(v.y), math.abs(v.z), math.abs(v.a), math.abs(v.b)}
17}
18
19fn (v Vec5) generic_new(f fn (f64) f64) Vec5 {
20 return Vec5{f(v.x), f(v.y), f(v.z), f(v.a), f(v.b)}
21}
22
23fn test_generic_function_argument_inference() {
24 v := vec5_from(-1.0)
25 assert v.generic_new(math.abs) == v.abs_oldstyle()
26}
27