[erlang-questions] Exporting a record type

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Thu Jul 16 10:24:56 CEST 2015


On Thu, Jul 16, 2015 at 1:02 AM, <lloyd@REDACTED> wrote:

> 1) I'm guessing repr is your abbreviation for replica and rel for
> relation. Is this correct?
>

 Dmitry is right on both accounts here. `repr` is a shorthand for
representation, i.e., how to represent the data.

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?
>

In languages with static types, you eventually have to make a choice for a
type 'book'. If we export this type and use it in other modules, are these
other modules allowed to the see the structure of a book and work with that
structure or are they not? The former is called a transparent ascription
and the latter is called an opaque ascription.

The reason you may want to use opaque ascriptions is that you can change
the internal representation. Say you have defined

type 'a queue = 'a list
let empty = []
let push x q = List.append q [x]
let pop q =
    match q with
    | [] -> None
    | (x :: q) -> Some (x, q)

In something like OCaml. Now, if another module abuses the fact that a
queue is a list, then you can't change the implementation later on. So you
mark the queue as opaque which means any user of the queue *has* to use the
functions empty/push/pop defined in the module. We can thus later change
the internal representation to, say,

type 'a queue =
    { front : 'a list ;
      back : 'a list }

and change the functions accordingly, without breaking any other user of
the module as we forced them into a mode where they cannot inspect the
internal representation of the queue.

The -opaque annotation serves the same purpose in Erlang, and this is also,
what Dmitry said :)





-- 
J.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150716/0d54fbde/attachment.htm>


More information about the erlang-questions mailing list