[erlang-questions] Exporting a record type

lloyd@REDACTED lloyd@REDACTED
Fri Jul 10 18:11:18 CEST 2015


Hi Gordon,

The why is beginning to penetrate. The how is still murky. Examples definitely help.

If I understand your riak example, it looks to me like the module riak_object.erl, among other things, defines a record called #r_object{...}, then employs the -opaque  declaration to hide the structure of #r_object{}  behind a veil, giving it an alias, riak_object() (line 67).

But as I scan further down the module, I see riak_object() used in many -spec declarations but not in a single function definition; while #r_object{...} shows up in numerous functions (Lines 135, 144, 151, etc.).

Is my understanding correct that one would strive to define all functions accessing #r_object{...) directly in and only in the module riak_object and it is these functions that would be exported?

I see that the alias riak_object() is exported. How would it, then, be called in other modules to populate or access #r_object{}?

Here's my case:

In the module wg_dets_books.erl I've defined:

-record(book, {
   isbn,
   member_id,
   date=calendar:universal_time(),
   title,
   category,
   gentre,
   author,
   publisher,
   pubDate,
   smallImageURL,
   largeImageURL,
   squib,
   coverCopy
   }).

-opaque book() :: #book{}.

-export_type([book/0]).

In another module I query a remote db that returns:

   Title        = get_title(Xml),
   Author       = get_author(Xml),
   Publisher    = get_publisher(Xml),
   PubDate     = get_pub_date(Xml),
   SmallImage  = get_small_image(Xml),
   LargeImage  = get_large_image(Xml),
   CoverCopy  = get_cover_copy(Xml),

So, now what syntax would I use in this module to populate the book record?

Apologies to all for my dense mentality, but this seems like a fundamental and important matter of Erlang craft that I want to understand thoroughly.

All the best,

Lloyd



   

-----Original Message-----
From: "Gordon Guthrie" <gguthrie@REDACTED>
Sent: Friday, July 10, 2015 11:18am
To: "Lloyd R. Prentice" <lloyd@REDACTED>
Cc: "Jesper Louis Andersen" <jesper.louis.andersen@REDACTED>, "erlang-questions" <erlang-questions@REDACTED>
Subject: Re: [erlang-questions] Exporting a record type

Maybe an example would help.

This is the riak_object from Basho’s riak and it is an encapsulated record with exported types and transactional functions that operate on the opaque object:
https://github.com/basho/riak_kv/blob/develop/src/riak_object.erl <https://github.com/basho/riak_kv/blob/develop/src/riak_object.erl>

It is written like this because it is passed around the whole system and persisted to the metal, so opacity and forward/backward changes need to be able to be dropped into the code base with rewriting *everything*

Gordon


> Le 10 juil. 2015 à 16:05, Lloyd R. Prentice <lloyd@REDACTED> a écrit :
> 
> Thanks Jesper and to all who have so generously responded.
> 
> Jesper, can you please expand on the following:
> 
> "Generally you want to avoid module 'foo' to contain getter/setter pairs and define the "transactional" states that can happen to 't'."
> 
> Where would one define getter/setter pairs? What do you mean by "transactional" states?
> 
> Many thanks,
> 
> Lloyd
> 
> 
> Sent from my iPad
> 
>> On Jul 10, 2015, at 7:25 AM, Jesper Louis Andersen <jesper.louis.andersen@REDACTED> wrote:
>> 
>> Generally you want to avoid module 'foo' to contain getter/setter pairs and define the "transactional" states that can happen to 't'.
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions






More information about the erlang-questions mailing list