kzemek
Es6_maps - EcmaScript6-style shorthand map literals
I’ve just released what I believe is a production-ready version of my library, es6_maps.
es6_maps hacks into the Elixir compiler to enable EcmaScript6-like shorthand map usage that you might know from JavaScript or Rust:
iex> foo = 1
iex> %{hello, bar} = %{foo, hello: "world", bar: 2}
%{foo: 1, hello: "world", bar: 2}
iex> hello
"world"
There’s already a popular library doing a similar thing, shorter_maps, that doesn’t need any module-replacing shenanigans in order to do its thing. The motivation for implementing es6_maps instead of using shorter_maps was that:
- I do firmly believe this is a good language feature and a very natural extension of map literals syntax;
- at the same time, being a language feature it should be simple -
es6_mapsworks only with atom keys and has no extra features over the key expansion.
Point 1 is additionally reinforced by how easy it was to introduce to the compiler - I’m injecting just 9 lines of simple code, while parser and lexer already accept short-form maps without modifications.
The library also includes a Mix task for jumping all-in to shorthand map forms - as well as reverting all of the shorthand literals back into expanded ones in case you’d like to remove the dependency.
GitHub: GitHub - kzemek/es6_maps: `%{foo, bar: 1}` — ES6-like shorthand syntax for Elixir maps and structs
Hex: es6_maps | Hex
HexDocs: es6_maps v1.0.1 — Documentation
Most Liked
kzemek
I have released the v1.0.0 version of es6_maps!
The major changes are:
es6_mapsnow amends the Elixir compiler at a different point (:elixir.string_to_tokens/5), which improves compatibility with other Elixir tools and happens to be a more stable API.- It can now be used at runtime, so we can paste in shorthand syntax to deployed shells
- It includes an ElixirLS plugin to ensure it’s active while the language server analyzes the code.
The v1.0.0 is also meant to signal production readiness. The library has been battle tested over the past year, and with the changes above I’m confident of its stability going forward.
GitHub: GitHub - kzemek/es6_maps: ES6-like shorthand syntax for Elixir maps: `%{foo, bar} = map; IO.puts(foo)`
Hex: es6_maps | Hex
HexDocs: es6_maps v1.0.0 — Documentation
LostKobrakai
This discussion is at least as old as elixir is stable. This is from just a few month after 1.0 and it still mentions earlier proposals as well: https://groups.google.com/g/elixir-lang-core/c/NoUo2gqQR3I
sodapopcan
I believe this is the most recent extended discussion complete with proposal and poll: https://elixirforum.com/t/proposal-add-field-puns-map-shorthand-to-elixir/15452







