Time for more Erlang! In this very special episode, we’ll be talking about Records and Macros, as covered in Getting Started with Erlang.

Erlang Header Files

The chapter talks about Erlang header files – files with the file extension .hrl by convention. These files are used to contain configuration data and data structure/interface definitions.

Header files are used in implementation files (.erl) as such:


This should look familiar to you if you’ve ever used a language with a preprocessor.


I know I said “Records and Macros” (and that’s the title of the chapter), but I’m going to cover macros first, as they’re simpler. Configuration data often takes the form of macros, and a macro is created as such:

-define(Const, Replacement).

This is very similar to #define in C-family languages in that the preprocessor will replace every instance of Const with Replacement in the code, except that Const must be preceded by a questions mark:

-define(my_macro, my_replacement).

main() ->
    io:format("~p~n", [?my_macro]).

The above would output my_replacement.

There are a couple of predefined macros, the details of which can be found here.


Records are more interesting than macros – the provide a way to define tuple data structures in a reusable way throughout your code.

A record is defined similarly to a macro, usually in a header file:

-record(record_name, {field_name}).
-record(other_record, {field_with_default=0}).

Records can then be created in functions using the field names:

main() ->
    MyRecord = #record_name{field_name=10},
    OtherRecord = #other_record{}.

To read more about records and the other fancy things they do, check out the docs.

It’s a little hard to see how this all fits together and how it is useful, so I recommend reading the actual chapter, where the messaging app example is modified to use records and macros to simplify some of the message passing.

That’s it for now!

— M