v / vlib / v2 / gen / cleanc / tests / generic_sum_cast.v
56 lines · 50 sloc · 989 bytes · f3c5760b8838272e4789c38a1be9e03f9ceac351
Raw
1struct Point {
2 x int
3}
4
5type Primitive = []int | []string | []Point | bool | int | string | Point
6
7fn wrap_first[T](values []T) Primitive {
8 if values.len > 0 {
9 first := values[0]
10 return Primitive(first)
11 }
12 return Primitive(false)
13}
14
15fn check(value Primitive) {
16 match value {
17 []int {
18 int_value := wrap_first(value)
19 if int_value is int {
20 println('int:${int_value}')
21 } else {
22 println('int:bad')
23 }
24 }
25 []string {
26 string_value := wrap_first(value)
27 if string_value is string {
28 println('string:${string_value}')
29 } else {
30 println('string:bad')
31 }
32 }
33 []Point {
34 point_value := wrap_first(value)
35 if point_value is Point {
36 println('point:${point_value.x}')
37 } else {
38 println('point:bad')
39 }
40 }
41 else {}
42 }
43}
44
45fn main() {
46 ints := []int{len: 1, init: 7}
47 check(Primitive(ints))
48
49 strings := []string{len: 1, init: 'abc'}
50 check(Primitive(strings))
51
52 points := []Point{len: 1, init: Point{
53 x: 3
54 }}
55 check(Primitive(points))
56}
57