Currently, Elixir map syntax does not support anything other than pairs and macro calls as explained here:
Unfortunately, this restriction is at the parser level, so there's very little freedom when writing own macros.
My
suggestion is to relax this restriction and to let the macros decide the
interpretation for themselves. The compiler would be still free to fail if not happy with the resulting AST.
The use case that made me reach out is my
destructuring library where I need the following syntax and not just for the
lists and tuples, but for maps as well (the <~ operator
implementation is mine):
%{ foo \\ 25} <~ assigns
or
%{
false: dropped_keys \\ [],
true: kept_keys \\ []
} <~ Enum.group_by( all_keys, & &1 in keys)
Not
having the ability to do this, my library implementation currently relies on the macro
syntax at the expense of readability and in doing so it: a) diverges from the Elixir convention for optional arguments, and b) prohibits the user to use their macros in the
left-side expression, as in:
%{ foo( 25)} <~ assigns
or
%{
false: dropped_keys( []),
true: kept_keys( [])
} <~ Enum.group_by( all_keys, & &1 in keys)
It's true I
could've opted for using the \\ operator for lists and tuples while keeping the macro call syntax for the maps but I didn't want to do it as the idea is to have a uniform way of declaring optional variables.
Please note that my library is just one example of why this may be useful and I am sure there are other people who would like more freedom in creating their own macros.
Thanks
PS. The library:
https://github.com/DaTrader/extructure