blob: 879b873ebaf812ba4741a9ecd8f9474dbabf759f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
interface Speaker {
say_hello() string
speak(msg string)
}
struct Boss {
name string
}
fn (b Boss) say_hello() string {
return 'Hello, My name is $b.name and I\'m the bawz'
}
fn (b Boss) speak(msg string) {
println(msg)
}
struct Cat {
name string
breed string
}
fn (c Cat) say_hello() string {
return 'Meow meow $c.name the $c.breed meow'
}
fn (c Cat) speak(msg string) {
println('Meow $msg')
}
struct Baz {
mut:
sp Speaker
}
fn test_interface_struct() {
bz1 := Baz{
sp: Boss{
name: 'Richard'
}
}
assert bz1.sp.say_hello() == "Hello, My name is Richard and I'm the bawz"
print('Test Boss inside Baz struct: ')
bz1.sp.speak('Hello world!')
bz2 := Baz{
sp: Cat{
name: 'Grungy'
breed: 'Persian Cat'
}
}
assert bz2.sp.say_hello() == 'Meow meow Grungy the Persian Cat meow'
print('Test Cat inside Baz struct: ')
bz2.sp.speak('Hello world!')
}
fn test_interface_mut_struct() {
mut mbaz := Baz{
sp: Boss{
name: 'Derek'
}
}
assert mbaz.sp.say_hello() == "Hello, My name is Derek and I'm the bawz"
mbaz.sp = Cat{
name: 'Dog'
breed: 'Not a dog'
}
assert mbaz.sp.say_hello() == 'Meow meow Dog the Not a dog meow'
}
fn test_interface_struct_from_array() {
bazs := [
Baz{
sp: Cat{
name: 'Kitty'
breed: 'Catty Koo'
}
},
Baz{
sp: Boss{
name: 'Bob'
}
},
]
assert bazs[0].sp.say_hello() == 'Meow meow Kitty the Catty Koo meow'
assert bazs[1].sp.say_hello() == "Hello, My name is Bob and I'm the bawz"
}
/*
// TODO: fix this too; currently with V 0.1.30 7426544 produces: `V panic: as cast: cannot cast 200 to 197`
fn test_interface_struct_from_mut_array() {
mut bazs := [
Baz{
sp: Cat{
name: 'Kitty'
breed: 'Catty Koo'
}
},
Baz{
sp: Boss{
name: 'Bob'
}
}
]
bazs[0].sp = Boss{
name: 'Ross'
}
bazs[1].sp = Cat{
name: 'Doggy'
breed: 'Doggy Doo'
}
assert bazs[0].sp.say_hello() == 'Hello, My name is Ross and I\'m the bawz'
assert bazs[1].sp.say_hello() == 'Meow meow Doggy the Doggy Doo meow'
}
*/
|