| 1 | type Vector = []f64 |
| 2 | |
| 3 | @[heap] |
| 4 | struct HeapArray { |
| 5 | mut: |
| 6 | data []f64 |
| 7 | } |
| 8 | |
| 9 | fn new_vector(x f64, y f64, z f64, w f64) &Vector { |
| 10 | a := HeapArray{ |
| 11 | data: [x, y, z, w] |
| 12 | } |
| 13 | return &Vector(&a.data) |
| 14 | } |
| 15 | |
| 16 | pub fn (a &Vector) + (b &Vector) &Vector { |
| 17 | mut res := HeapArray{ |
| 18 | data: []f64{len: a.len} |
| 19 | } |
| 20 | for i := 0; i < a.len; i++ { |
| 21 | res.data[i] = (*a)[i] + (*b)[i] |
| 22 | } |
| 23 | return &Vector(&res.data) |
| 24 | } |
| 25 | |
| 26 | fn test_alias_array_plus_overload() { |
| 27 | mut a := new_vector(12, 4.5, 6.7, 6) |
| 28 | b := new_vector(12, 4.5, 6.7, 6) |
| 29 | dump(a) |
| 30 | dump(b) |
| 31 | c := a + b |
| 32 | dump(c) |
| 33 | assert *c == Vector([f64(24), 9, 13.4, 12]) |
| 34 | a = a + b |
| 35 | assert a.str() == c.str() |
| 36 | dump(a) |
| 37 | } |
| 38 | |