-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.d
59 lines (48 loc) · 1.23 KB
/
app.d
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
import openmethods; // import lib
mixin(registerMethods); // once per module - don't forget!
interface Animal {}
class Dog : Animal {}
class Pitbull : Dog {}
class Cat : Animal {}
class Dolphin : Animal {}
// open method with single argument <=> virtual function "from outside"
string kick(virtual!Animal);
@method // implement 'kick' for dogs
string _kick(Dog x) // note the underscore
{
return "bark";
}
@method("kick") // use a different name for specialization
string notGoodIdea(Pitbull x)
{
return next!kick(x) ~ " and bite"; // aka call 'super'
}
// multi-method
string meet(virtual!Animal, virtual!Animal);
// 'meet' implementations
@method
string _meet(Animal, Animal)
{
return "ignore";
}
@method
string _meet(Dog, Dog)
{
return "wag tail";
}
@method
string _meet(Dog, Cat)
{
return "chase";
}
void main()
{
import std.stdio;
Animal hector = new Pitbull, snoopy = new Dog;
writeln("kick snoopy: ", kick(snoopy)); // bark
writeln("kick hector: ", kick(hector)); // bark and bite
Animal felix = new Cat, flipper = new Dolphin;
writeln("hector meets felix: ", meet(hector, felix)); // chase
writeln("hector meets snoopy: ", meet(hector, snoopy)); // wag tail
writeln("hector meets flipper: ", meet(hector, flipper)); // ignore
}