-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.nim
50 lines (35 loc) · 1.18 KB
/
test.nim
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
import interfaced
import test_exports
type
Human = ref object
name: string
Dog = ref object
proc makeNoise(human: Human): string =
"Hello, my name is " & human.name
proc legs(human: Human): int = 2
proc greet(human: Human, other: string): string =
"Nice to meet you, " & other
proc makeNoise(dog: Dog): string = "Woof! Woof!"
proc legs(dog: Dog): int = 4
proc greet(dog: Dog, other: string): string = "Woof! Woooof... wof!?"
createInterface(Animal):
proc makeNoise(this: Animal): string
proc legs(this: Animal): int
proc greet(this: Animal, other: string): string
proc interact(animal: Animal) =
echo animal.makeNoise
echo animal.greet("James Bond")
proc interactAll(animals: varargs[Animal, toAnimal]) =
for animal in animals:
animal.interact()
when isMainModule:
var
me = Human(name: "Andrea")
bau = Dog()
charmander = Charmander()
espeon = Espeon()
for animal in @[me.toAnimal, bau.toAnimal, charmander.toAnimal, espeon.toAnimal]:
echo "Number of legs: ", legs(animal)
for pokemon in @[charmander.toPokemon, espeon.toPokemon]:
echo pokemon.name(), " is a ", pokemon.typ(), " Pokemon"
interactAll(me, bau, charmander, espeon)