[erlang-questions] macro definition map for both assignment and matching

Frans Schneider schneider@REDACTED
Thu Sep 25 17:48:23 CEST 2014


Hi list,

I am using maps to represent attributes of an entity and use macro's to 
simplify the code.
As an example:

-define(MEDIA_URL_ATTRS(Media_type, Url, Name, Size),
         #{'type' => Media_type,
           'url' => Url,
           'file' => Name,
           'size' => Size}).

This is great for creating the maps, but now I also want to use the 
macro's for pattern matching which requires the => to become :=. as in

handle_call(?MEDIA_URL_ATTRS(Media_type, Url, Name, Size), ....) ->
     ...

Of course, I could just copy / paste / global replace / rename the 
macro's to a another set of macro's, but that seems rather silly. There 
are approximately 200 macro's, many of them nested, which makes 
maintaining the code an issue.

Any suggestions on making this work?

Frans

PS
I started out using lists for the attributes, but matching only works if 
the order of the attributes can be guaranteed, which is not the case. 
Also, partial matching works rather nice with maps.



More information about the erlang-questions mailing list