Skip to content

Instantly share code, notes, and snippets.

@fmamud
Last active October 27, 2015 18:55
Show Gist options
  • Save fmamud/7b77f0bb45b0fc2b491c to your computer and use it in GitHub Desktop.
Save fmamud/7b77f0bb45b0fc2b491c to your computer and use it in GitHub Desktop.
Erlang Records Module
-module(records).
-compile(export_all).
-include("records.hrl").
-record(robot, {name,
type=industrial,
hobbies,
details=[]}).
-record(user, {id, name, group, age}).
first_robot() ->
#robot{name="Mechatron",
type=handmade,
details=["Moved by a small man inside"]}.
car_factory(CorpName) ->
#robot{name=CorpName, hobbies="building cars"}.
%% use pattern matching to filter
admin_panel(#user{name=Name, group=admin}) ->
Name ++ " is allowed!";
admin_panel(#user{name=Name}) ->
Name ++ " is not allowed".
%% can extend user without problem
adult_section(U = #user{}) when U#user.age >= 18 ->
%% Show stuff that can't be written in such a text
allowed;
adult_section(_) ->
%% redirect to sesame street site
forbidden.
repairman(Rob) ->
Details = Rob#robot.details,
NewRob = Rob#robot{details=["Repaired by repairman"|Details]},
{repaired, NewRob}.
included() -> #included{some_field="Some value"}.
%% this is a .hrl (header) file.
-record(included, {some_field,
some_default = "yeah!",
unimaginative_name}).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment