[erlang-questions] maps or records?

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Fri Feb 26 17:03:21 CET 2016


On Fri, Feb 26, 2016 at 4:23 PM, Siraaj Khandkar <siraaj@REDACTED>
wrote:

> When keys are defined statically (i.e. they do not change at run time),
> such as process state, a tuple (such as an Erlang record) is the
> semantically-correct choice.


Another important point is that all keys needs to have valid data always.
Otherwise you may have a key mapping to undefined, and that is often an
example of illegal state you can represent.

In general I try to limit the amount of knowledge of records outside its
using module and at most put them locally in an application-scoped .hrl (in
the src dir). Records bind tightly in the sense that any module using a
record needs to know about its structure.

For ad-hoc returns, I sometimes use a map rather than a tuple. enacl has an
example of you generate a key:

#{ public := PK, secret := SK } = enacl:box_keypair(),

which makes it harder to accidentally swap the PK and SK of a keypair since
you are forced to say what goes first and what goes second. In general, I
tend to do this for things where you have multiple values of the same type
in statically typed languages, but in dynamically typed languages I
sometimes do this even more often to avoid swaps.


-- 
J.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160226/8974e4fb/attachment.htm>


More information about the erlang-questions mailing list