<div dir="ltr"><div class="gmail_default" style="font-family:verdana,sans-serif">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.</div><div class="gmail_default" style="font-family:verdana,sans-serif">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:</div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif">-module(shoes).</div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif">-export([new/2, id/1, color/1, color/2, size/1, size/2]).</div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif">-record(shoe, {id = new_id(), color, size}).<br></div><div class="gmail_default" style="font-family:verdana,sans-serif">-opaque shoe() :: #shoe{}.</div><div class="gmail_default" style="font-family:verdana,sans-serif">-export_type([shoe/0]).</div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif">new(Color, Size) -> #shoe{color = Color, size = Size}.</div><div class="gmail_default" style="font-family:verdana,sans-serif">id(#shoe{id = Id}) -> Id.</div><div class="gmail_default" style="font-family:verdana,sans-serif">color(#shoe{color = Color}) -> Color.</div><div class="gmail_default" style="font-family:verdana,sans-serif">color(Shoe, Color) -> Shoe#shoe{color = Color}.</div><div class="gmail_default" style="font-family:verdana,sans-serif">size(#shoe{size = Size}) -> Size.</div><div class="gmail_default" style="font-family:verdana,sans-serif">size(Shoe, Size) -> Shoe#shoe{size = Size}.</div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif">new_id() -> implement:it(yourself).</div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif">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:</div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif">filter_by_size(Shoes, Size) -></div><div class="gmail_default" style="font-family:verdana,sans-serif"> [Shoe || Shoe <- Shoes, shoes:size(Shoe) == Size].</div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif">Hope this helps.</div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"><div><font face="verdana, sans-serif"><br></font></div><font face="verdana, sans-serif"><br></font><blockquote style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:40px;border-top-style:none;border-right-style:none;border-bottom-style:none;border-left-style:none;border-width:initial;border-color:initial;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px"><div style="text-align:right"><span style="font-family:verdana,sans-serif"><i><b><a href="http://google.com/profiles/greenmellon" target="_blank">Fernando Benavides</a></b></i></span></div></blockquote></div></div>
<br><div class="gmail_quote">On Thu, Jul 9, 2015 at 5:17 PM, Bob Ippolito <span dir="ltr"><<a href="mailto:bob@redivi.com" target="_blank">bob@redivi.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span class="">On Thu, Jul 9, 2015 at 5:05 PM, <span dir="ltr"><<a href="mailto:lloyd@writersglen.com" target="_blank">lloyd@writersglen.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
In Loïc's new book, The Erlanger Playbook, he advocates against using including records through *.hrl. (pp 40-41)<br>
<br>
Instead, he recommends using types:<br>
<br>
-record(state, {}).<br>
-opaque state() :: #state{}.<br></blockquote><div><br></div></span><div>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.</div><span class=""><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
He leaves hanging the question of how to access this record from another module.<br>
<br>
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.<br>
<br>
My case and problem is as follows:<br>
<br>
-record(book, {<br>
... yada yada (some seven fields)<br>
}<br>
<br>
I define the record, then:<br>
<br>
-opaque book() :: #book{}.<br>
<br>
-export_type([book/0]).<br>
<br>
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.<br></blockquote><div><br></div></span><div>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.</div><div><br></div><div>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.</div><span class="HOEnZb"><font color="#888888"><div><br></div><div>-bob</div><div><br></div></font></span></div></div></div>
<br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div>