[erlang-questions] maps or records?

Siraaj Khandkar siraaj@REDACTED
Fri Feb 26 17:25:01 CET 2016


On 2/26/16 11:03 AM, Jesper Louis Andersen wrote:
> 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.
>

Besides Dialyzer, there's also this neat trick to assert that required 
fields are not accidentally left-out:

   -record(foo, {field_a = error(field_a_is_required_in_foo) :: a()}).

Which I learned from Adam Rutkowski and Marcin Górnik.



More information about the erlang-questions mailing list