-
Notifications
You must be signed in to change notification settings - Fork 0
/
oop.erl
28 lines (24 loc) · 828 Bytes
/
oop.erl
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
-module(oop).
-export([animal/1, dog/1, cat/1]).
%% all the method calls need to be in tuples when they have more than
%% one argument so we can use functions of arity 1 for every call we make.
animal(Name) ->
fun(type) -> "living thing";
(name) -> Name;
(move) -> Name++" moves around...";
({eat, Item}) -> Name++" eats "++Item;
(_) -> "I'm sorry Dave, I can't do that."
end.
dog(Name) ->
Parent = animal(Name),
fun(talk) -> Name++" says: Woof!";
({chase, Animal}) when is_function(Animal) ->
Name++" chases a "++Animal(type)++" named "++Animal(name)++" around";
(X) -> Parent(X)
end.
cat(Name) ->
Parent = animal(Name),
fun(type) -> "cat";
(talk) -> Name++" says: Meow!";
(X) -> Parent(X)
end.