[PROPOSAL] Map deconstruction syntax

33 views
Skip to first unread message

Joshua Nussbaum

unread,
Feb 11, 2020, 2:58:45 AM2/11/20
to elixir-lang-core
Hi,

What do you think about adding map deconstruction syntax similar to JavaScript. It would remove the need to declare the key and binding variable when they are identical names.

Often the map keys and binding var use the same name:

# without deconstruction
%{name: name, email: email} = user

vs

# with deconstruction (identical)
%{name, email} = user

This would work too for function definitions `def send_email(%{name, email}) do ...`, pattern matching ` case user, do: %{name, email} -> ....` as well as updates `%{user | email, name}`.

It would also require updating `mix format` to replace `%{key: key}` with `%{key}`

Here is a prototype of the changes required to the compiler:
Josh

 

Fernando Tapia Rico

unread,
Feb 11, 2020, 3:31:56 AM2/11/20
to elixir-lang-core
Hi Joshua,

This has been suggested a few times in the mailing list: https://groups.google.com/forum/#!searchin/elixir-lang-core/short_maps%7Csort:date

Hope that helps :)

Joshua Nussbaum

unread,
Feb 11, 2020, 3:44:48 AM2/11/20
to elixir-lang-core
Thanks Fernando,

I should have searched first :)
Reply all
Reply to author
Forward
0 new messages