[erlang-questions] Exporting a record type

Bob Ippolito bob@REDACTED
Fri Jul 10 02:17:08 CEST 2015


On Thu, Jul 9, 2015 at 5:05 PM, <lloyd@REDACTED> wrote:

>
> In  Loïc's new book, The Erlanger Playbook, he advocates against using
> including records through *.hrl. (pp 40-41)
>
> Instead, he recommends using types:
>
> -record(state, {}).
> -opaque state() :: #state{}.
>

This looks like the fairly common recommendation of: don't export records.
Treat them as an opaque data type, and provide functions for working with
them.


> He leaves hanging the question of how to access this record from another
> module.
>
> The Erlang Types and Function Specifications doc suggests that the type
> can be exported using -export_type(...). But, to my feeble mind, the
> exposition is less than clear.
>
> My case and problem is as follows:
>
> -record(book, {
>    ... yada yada (some seven fields)
>    }
>
> I define the record, then:
>
> -opaque book() :: #book{}.
>
> -export_type([book/0]).
>
> Now, I access a remote db in a different module and want to instantiate a
> book record, but it's not at all clear to me how to do so.
>

You don't. You have a function in the module that defines book that can
create a book record, and other functions to manipulate them.

An example of this style would be the dict module. It uses records
internally to implement a dictionary data structure, but you don't have
direct access to the record(s) that it uses. Just functions to work with
them.

-bob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150709/c04bf820/attachment.htm>


More information about the erlang-questions mailing list