[erlang-questions] Exporting a record type
Loïc Hoguin
essen@REDACTED
Fri Jul 10 10:11:22 CEST 2015
I guess I need to expand on that in the next release. :-)
Sorry I couldn't answer sooner, barely keeping up with everything.
On 07/10/2015 03:51 AM, Lloyd R. Prentice wrote:
> Thanks Bob and Fernando,
>
> Fernando, your code example is a great help.
>
> Thanks again,
>
> Lloyd
>
> Sent from my iPad
>
> On Jul 9, 2015, at 8:56 PM, Fernando Benavides <elbrujohalcon@REDACTED
> <mailto:elbrujohalcon@REDACTED>> wrote:
>
>> Loïc will correct me if I'm wrong but the idea is to use records
>> *only* as an internal representation of your data types.
>> For instance, let's say in your system you have shoes (for lack of a
>> better thing). You'll then create a module called shoes.erl:
>>
>> -module(shoes).
>>
>> -export([new/2, id/1, color/1, color/2, size/1, size/2]).
>>
>> -record(shoe, {id = new_id(), color, size}).
>> -opaque shoe() :: #shoe{}.
>> -export_type([shoe/0]).
>>
>> new(Color, Size) -> #shoe{color = Color, size = Size}.
>> id(#shoe{id = Id}) -> Id.
>> color(#shoe{color = Color}) -> Color.
>> color(Shoe, Color) -> Shoe#shoe{color = Color}.
>> size(#shoe{size = Size}) -> Size.
>> size(Shoe, Size) -> Shoe#shoe{size = Size}.
>>
>> new_id() -> implement:it(yourself).
>>
>> Whenever you need to work with shoes anywhere in your system, you just
>> use the functions exported by the module. Let's say you have a list of
>> shoes and you only want those of a particular size:
>>
>> filter_by_size(Shoes, Size) ->
>> [Shoe || Shoe <- Shoes, shoes:size(Shoe) == Size].
>>
>> Hope this helps.
>>
>>
>>
>> /*Fernando Benavides <http://google.com/profiles/greenmellon>*/
>>
>>
>> On Thu, Jul 9, 2015 at 5:17 PM, Bob Ippolito <bob@REDACTED
>> <mailto:bob@REDACTED>> wrote:
>>
>> On Thu, Jul 9, 2015 at 5:05 PM, <lloyd@REDACTED
>> <mailto: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
>>
>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED <mailto:erlang-questions@REDACTED>
>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>>
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
--
Loïc Hoguin
http://ninenines.eu
Author of The Erlanger Playbook,
A book about software development using Erlang
More information about the erlang-questions
mailing list