| 1 |
%% File: person.hrl |
| 2 |
|
| 3 |
%%----------------------------------------------------------- |
| 4 |
%% Data Type: person |
| 5 |
%% where: |
| 6 |
%% name: A string (default is undefined). |
| 7 |
%% age: An integer (default is undefined). |
| 8 |
%% phone: A list of integers (default is []). |
| 9 |
%% dict: A dictionary containing various information |
| 10 |
%% about the person. |
| 11 |
%% A {Key, Value} list (default is the empty list). |
| 12 |
%%------------------------------------------------------------ |
| 13 |
-record(person, {name, age, phone = [], dict = []}). |
| 14 |
|
| 15 |
-module(person). |
| 16 |
-include("person.hrl"). |
| 17 |
-compile(export_all). % For test purposes only. |
| 18 |
|
| 19 |
%% This creates an instance of a person. |
| 20 |
%% Note: The phone number is not supplied so the |
| 21 |
%% default value [] will be used. |
| 22 |
|
| 23 |
make_hacker_without_phone(Name, Age) -> |
| 24 |
#person{name = Name, age = Age, |
| 25 |
dict = [{computer_knowledge, excellent}, |
| 26 |
{drinks, coke}]}. |
| 27 |
|
| 28 |
%% This demonstrates matching in arguments |
| 29 |
|
| 30 |
print(#person{name = Name, age = Age, |
| 31 |
phone = Phone, dict = Dict}) -> |
| 32 |
io:format("Name: ~s, Age: ~w, Phone: ~w ~n" |
| 33 |
"Dictionary: ~w.~n", [Name, Age, Phone, Dict]). |
| 34 |
|
| 35 |
%% Demonstrates type testing, selector, updating. |
| 36 |
|
| 37 |
birthday(P) when is_record(P, person) -> |
| 38 |
P#person{age = P#person.age + 1}. |
| 39 |
|
| 40 |
register_two_hackers() -> |
| 41 |
Hacker1 = make_hacker_without_phone("Joe", 29), |
| 42 |
OldHacker = birthday(Hacker1), |
| 43 |
% The central_register_server should have |
| 44 |
% an interface function for this. |
| 45 |
central_register_server ! {register_person, Hacker1}, |
| 46 |
central_register_server ! {register_person, |
| 47 |
OldHacker#person{name = "Robert", |
| 48 |
phone = [0,8,3,2,4,5,3,1]}}. |
| 49 |
|
| 50 |
%% From https://erlang.org/doc/programming_examples/records.html#a-longer-example |