Search code examples
erlangrecorderl

How to share a record between multiple erl in Erlang?


I would like to use a -record in every single erl files I have. I know I can repeat the record individually in every files, but that's really ugly.

Any suggestion ?

P.S. : Be gentle ;-) I'm an Erlang newbie.


Solution

  • Put your record definition in a header (.hrl) file. For example, animal.hrl may look like:

    -record(animal, {name, legs=4, eyes=2}).
    

    Then in your .erl files you can include the .hrl file like so:

    -include_lib("animal.hrl").
    
    • I'd recommend reading this.
    • Note that the .hrl file should probably be placed in your src directory unless it's needed outside of your application - in that case you should put it in a directory called include.