| 1 | fn func(mut a []int) { |
| 2 | a = [1, 2, 3, 4] |
| 3 | println('inside fn: ${a}') |
| 4 | assert '${a}' == '[1, 2, 3, 4]' |
| 5 | } |
| 6 | |
| 7 | fn test_fn_mut_args_of_array() { |
| 8 | mut a := [1, 2, 3] |
| 9 | func(mut a) |
| 10 | println('inside main: ${a}') |
| 11 | assert '${a}' == '[1, 2, 3, 4]' |
| 12 | } |
| 13 | |
| 14 | fn init_map(mut n map[string]int) { |
| 15 | n = { |
| 16 | 'one': 1 |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | fn test_fn_mut_args_of_map() { |
| 21 | mut m := map[string]int{} |
| 22 | init_map(mut m) |
| 23 | println(m) |
| 24 | assert m == { |
| 25 | 'one': 1 |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | struct MyData { |
| 30 | pub mut: |
| 31 | ar []int |
| 32 | } |
| 33 | |
| 34 | fn pass_array_mut(mut ar []int) int { |
| 35 | if ar.len > 0 && ar.last() == 99 { |
| 36 | return 99 |
| 37 | } |
| 38 | return 0 |
| 39 | } |
| 40 | |
| 41 | fn test_fn_mut_args_of_array_last() { |
| 42 | mut m := MyData{} |
| 43 | m.ar << 99 |
| 44 | assert pass_array_mut(mut m.ar) == 99 |
| 45 | } |
| 46 | |
| 47 | interface ChildInterface { |
| 48 | data int |
| 49 | } |
| 50 | |
| 51 | struct Child { |
| 52 | data int |
| 53 | } |
| 54 | |
| 55 | struct Parent { |
| 56 | mut: |
| 57 | children []ChildInterface |
| 58 | } |
| 59 | |
| 60 | fn (mut p Parent) add(mut x ChildInterface) { |
| 61 | p.children << x |
| 62 | } |
| 63 | |
| 64 | fn test_fn_mut_args_of_interface() { |
| 65 | mut x := Parent{} |
| 66 | x.add(mut Child{ data: 123 }) |
| 67 | println(x.children[0].data) |
| 68 | assert x.children[0].data == 123 |
| 69 | } |
| 70 | |
| 71 | struct LinuxFile { |
| 72 | } |
| 73 | |
| 74 | interface File { |
| 75 | } |
| 76 | |
| 77 | fn b(parent File) { |
| 78 | println(parent) |
| 79 | assert '${parent}' == 'File(LinuxFile{})' |
| 80 | } |
| 81 | |
| 82 | fn a(mut parent File) { |
| 83 | b(parent) |
| 84 | } |
| 85 | |
| 86 | fn test_fn_mut_args_of_interface2() { |
| 87 | mut file := LinuxFile{} |
| 88 | a(mut file) |
| 89 | } |
| 90 | |