| 1 | // this file is just to test the parser so there may be a |
| 2 | // bunch of stuff in here that does not really make sense |
| 3 | module main |
| 4 | |
| 5 | struct GenericStructA[T] { |
| 6 | field_a T |
| 7 | } |
| 8 | |
| 9 | struct GenericStructB[T,U] { |
| 10 | field_a T |
| 11 | field_b U |
| 12 | } |
| 13 | |
| 14 | fn fn_generic_a[T](param_a T, param_b string, param_c int) int { |
| 15 | println('fn_generic_a: ${param_a.type}, ${param_b.type}, ${param_c.type}') |
| 16 | return 1 |
| 17 | } |
| 18 | |
| 19 | fn fn_generic_b[T,Y](param_a T, param_b Y) int { |
| 20 | println('fn_generic_a: ${param_a.type}, ${param_b.type}') |
| 21 | } |
| 22 | |
| 23 | fn fn_generic_c[fn[U,I](U, I) U, Y](param_a T, param_b Y) int { |
| 24 | println('fn_generic_c') |
| 25 | return 1 |
| 26 | } |
| 27 | |
| 28 | fn test_generic_struct_init() { |
| 29 | a := GenericStructA[int] { |
| 30 | field_a: 1 |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | fn fn_generic_init_a[T](param_a T) { |
| 35 | // TODO: infer correct init after type checking |
| 36 | // eg. array, chan, map, or struct init. |
| 37 | mut a := T{} |
| 38 | for k, v in param_a { |
| 39 | a[k] = v |
| 40 | } |
| 41 | println(a) |
| 42 | } |
| 43 | |
| 44 | fn test_generic_init() { |
| 45 | fn_generic_init_a[map[string]string]({'a': 'apple'}) |
| 46 | } |
| 47 | |
| 48 | // assoc works with generic structs although |
| 49 | // this will probably never be supported or used |
| 50 | fn test_generic_assoc() { |
| 51 | struct_a := GenericStructB[int,int]{field_a: 1, field_b: 2} |
| 52 | assoc_struct_b := GenericStructB[int,int]{ |
| 53 | ...struct_b |
| 54 | field_a: 10 |
| 55 | field_b: 20 |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | fn test_generic_call() { |
| 60 | call_generic_a := fn_generic_a[GenericStructA](GenericStructA{}, 'string', 1) |
| 61 | fn_generic_b[int,int](1,2) |
| 62 | } |
| 63 | |
| 64 | fn test_generic_complex_or_nested() { |
| 65 | fn_generic_c[fn[U,I](U, I) U, I](fn[U,I](param_a U, param_b I) U {}, 1) |
| 66 | |
| 67 | fn_generic_b[GenericStructA[Y],int](GenericStructA[int]{}, 1) |
| 68 | fn_generic_b[GenericStructA[Y],GenericStructA[Y]](GenericStructA[int]{}, 1) |
| 69 | struct_a := GenericStructA[int]{field_a: 1} |
| 70 | |
| 71 | fn_generic_b[[]string,map[string]string{}](1, 1) // TODO: error |
| 72 | |
| 73 | fn_a := fn(param_a string, param_b int, param_c int) {} |
| 74 | fn_b := fn(param_a string, param_b int, param_c int) {} |
| 75 | fn_c := fn(param_a string, param_b int, param_c int) {} |
| 76 | fn_a('a', a < b, a < b, c) |
| 77 | fn_a('a', foo: a < b, a < b, c) |
| 78 | fn_b('a', fn_generic_c[fn[T,Y](int),int](1)) |
| 79 | fn_b('a', fn_generic_c[fn[T,Y](int),int](a < if (fn_generic_b[int,int](1,2) > 2) { 1 } else { 2 }, 2), 1) |
| 80 | fn_b('a', moda.fn_generic_b[fn[T,Y](int),int](a < if (fn_generic_b[int,int](1,2) > 2) { 1 } else { 2 }, 2), fn_generic_b[int,int](1,2)) |
| 81 | fn_b('a', modb.submodb.fn_generic_b[int,int](fn_generic_b[int,int](fn_generic_b[int,int](1,2) < (fn_generic_b[int,int](1,2) - 2), 2)),fn_generic_b[int,int](1,2)) |
| 82 | |
| 83 | fn_c(fn_generic_b[GenericStructB[int,int]](GenericStructB<int>{}, 1), moda.fn_generic_b[GenericStructB[int,int]](GenericStructB[int]{}, 1)) |
| 84 | fn_c(a < fn_generic_b[GenericStructB[GenericStructA[int],int]](GenericStructB[int]{}, 1), moda.fn_generic_b[GenericStructB[int,int]](GenericStructB<int>{}, 1)) |
| 85 | |
| 86 | // return if x < 64 { fn_generic_b[int,int](1,2) } else { fn_generic_b[int,int](1, 2) < (fn_generic_b[int,int](1, 2) - 2) } |
| 87 | return fn_generic_b[int,int](1, 2) < b, c > d, e < f, g > h, fn_generic_b[int,int](fn_generic_b[int,int](fn_generic_b[int,int](1, 2), 2), 2) > j, k < l, m |
| 88 | } |
| 89 | |
| 90 | |