[erlang-questions] Exporting a record type

Dmitry Belyaev be.dmitry@REDACTED
Thu Jul 16 01:37:27 CEST 2015


-- 
Best wishes,
Dmitry Belyaev

On 16 July 2015 9:02:42 AM AEST, lloyd@REDACTED wrote:
>
>1) I'm guessing repr is your abbreviation for replica and rel for
>relation. Is this correct?

I think repr means "represent", so the result is a relation representation.

>
>3) Pardon my thick skull, but the book/0 declaration in the -opaque and
>-export_type attributes still puzzles me. Can I use book() in external
>modules? But without a parameter for a book instance, just exactly how
>would it be used?
>

Opaque means a value of the type must not be deconstructed in any other module. It only makes sense if you export it using "-export_type". You can freely use this exported type in any other module. If you try to peek inside such a value dialyzer will complain.

So in another module you'd write:

-spec author_name(book:book()) -> binary().
do(Book) ->
    Author = book:author(Book),
    book_author:name(Author).

>Much much appreciate your help.
>
>Lloyd
>
>
>
>-----Original Message-----
>From: "Jesper Louis Andersen" <jesper.louis.andersen@REDACTED>
>Sent: Monday, July 13, 2015 6:03am
>To: "Lloyd R. Prentice" <lloyd@REDACTED>
>Cc: "Gordon Guthrie" <gguthrie@REDACTED>, "erlang-questions"
><erlang-questions@REDACTED>
>Subject: Re: [erlang-questions] Exporting a record type
>
>On Sun, Jul 12, 2015 at 5:06 PM, Lloyd R. Prentice
><lloyd@REDACTED>
>wrote:
>
>> Also, in your repr/2 code (which is very suggestive of neat things
>one can
>> do) what is the significance of the View variable? As I see it now,
>it's
>> simply a tag like thumbnail_overview, but I'm not comfortable that my
>> understanding is correct.
>
>
>The idea, at least, is this: a "view" of some data, book, record, ...,
>is a
>way to cast that data into another structure by transforming it. I.e.,
>"view" the data in another form. In statically typed languages, some
>languages support built-in view-types, so you can define these
>transformations formally, but in Erlang you have to make a more dynamic
>resolution.
>
>In its simple form, the view is just an atom(), requesting the view.
>But in
>a more advanced form, the view is a language which can be executed to
>construct a view of the data:
>
>...
>repr(Book, {relation, R}) ->
>    repr_rel(Book, R).
>
>repr_rel(#book { genre = G } = Book, genre) ->
>    {G, Book}.
>
>Now, suppose you have a list of books, Bs:
>
>Rel = [repr(B, {relation, genre}) || B <- Bs],
>R = sofs:relation(Rel),
>F = sofs:relation_to_family(R),
>sofs:to_external(F).
>
>If for instance you have books [#book { genre = fantasy } = A, #book {
>genre = fantasy } = B, #book { genre = scifi } = C],
>
>Then the above would return
>
>[{fantasy, [A, B]}, {scifi, [C]}].
>
>There are numerous tricks that can be played here, depending on what
>transformations you need.




More information about the erlang-questions mailing list