[erlang-questions] Maps matching syntax - matching on keys that don't exist in a map - Alternative syntax?

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Wed Jun 4 15:46:36 CEST 2014


On Wed, Jun 4, 2014 at 2:09 PM, Rudolph van Graan <rvg.mailing@REDACTED>
wrote:

> Any suggestions?


You are modelling your data such that illegal data is representable. This
is usually a mistake based on the lack of proper sum-types in the language
of choice. The sum-types can be emulated in Erlang by use of tuples. Say we
have

#{ a => x, b => 8 } and #{ a => y, c => 9 }. Here, the 'a' key steers
whether or not the 'b' key and the 'c' key are valid. But if you use

-spec t() :: {x, integer()} | {y, integer()}.

as your representation, then you can match directly on the structure:

case Struct of
  {x, B} -> ...;
  {y, C} -> ...
end,

and there is no way we can represent the data #{ a => x, c => 9 } for
instance.

The resolution is to pick a better representation format of your data. And
to aggressively convert badly formed data at the system border.


-- 
J.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20140604/7d61cc98/attachment.htm>


More information about the erlang-questions mailing list