blob: a43cadfddaf580b779b6ce0316675643ba794289 (
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
|
struct Dog {
name string
age int
}
struct Cat {
name string
age int
}
interface Animal {
say(s string)
greet() int
}
fn (d Dog) say(s string) {
println('Dog $d.name: "$s"')
}
fn (c Cat) say(s string) {
println('Cat $c.name: "$s"')
}
fn (d Dog) greet() int {
d.say('Hello!')
return d.age
}
fn (c Cat) greet() int {
c.say('Hello!')
return c.age
}
fn use(a Animal) {
if a is Dog {
println('dog')
} else if a is Cat {
println('cat')
} else {
println('its a bug!')
}
}
fn main() {
use(Dog{'Doggo', 5})
use(Cat{'Nyancat', 6})
}
|