| 1 | struct Set[T] { |
| 2 | pub mut: |
| 3 | arr []T |
| 4 | } |
| 5 | |
| 6 | fn (mut s Set[T]) add(elem T) bool { |
| 7 | return match s.has(elem) { |
| 8 | true { |
| 9 | false |
| 10 | } |
| 11 | else { |
| 12 | s.arr << elem |
| 13 | true |
| 14 | } |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | fn (s Set[T]) has(elem T) bool { |
| 19 | return elem in s.arr |
| 20 | } |
| 21 | |
| 22 | fn test_generics_struct_with_array() { |
| 23 | mut s1 := Set[int]{} |
| 24 | println('declared a int-Set ${s1}') |
| 25 | s1.add(1) |
| 26 | println('the int-Set ${s1}') |
| 27 | assert s1.arr == [1] |
| 28 | |
| 29 | mut s2 := Set[bool]{} |
| 30 | println('declared a bool-Set ${s2}') |
| 31 | s2.add(true) |
| 32 | println('the bool-Set ${s2}') |
| 33 | assert s2.arr == [true] |
| 34 | |
| 35 | mut s3 := Set[f64]{} |
| 36 | println('declared a float-Set ${s3}') |
| 37 | s3.add(2.22) |
| 38 | println('the float-Set ${s3}') |
| 39 | assert s3.arr == [2.22] |
| 40 | |
| 41 | mut s4 := Set[string]{} |
| 42 | println('declared a string-Set ${s4}') |
| 43 | s4.add('smth') |
| 44 | println('the string-Set ${s4} ') |
| 45 | assert s4.arr == ['smth'] |
| 46 | } |
| 47 | |