kzemek

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:

  1. I do firmly believe this is a good language feature and a very natural extension of map literals syntax;
  2. at the same time, being a language feature it should be simple - es6_maps works 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 · GitHub
Hex: es6_maps | Hex
HexDocs: es6_maps v1.0.3 — Documentation

https://github.com/kzemek/es6_maps

Most Liked

kzemek

kzemek

I have released the v1.0.0 version of es6_maps!

The major changes are:

  • es6_maps now 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: `%{foo, bar: 1}` — ES6-like shorthand syntax for Elixir maps and structs · GitHub
Hex: es6_maps | Hex
HexDocs: es6_maps v1.0.3 — Documentation

LostKobrakai

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

sodapopcan

I believe this is the most recent extended discussion complete with proposal and poll: https://forum.elixirforum.com/t/proposal-add-field-puns-map-shorthand-to-elixir/15452

Where Next?

Popular in Announcing Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 30338 241
New
mindok
What is ContEx? A pure Elixir server-side data plotting/charting library outputting SVG. It has nice barcharts in particular and works g...
New
oltarasenko
Dear Elixir community, After a year of development, bug fixes, and improvements, we are proudly ready to share the release of Crawly 0.1...
New
Qqwy
TypeCheck: Fast and flexible runtime type-checking for your Elixir projects. Core ideas Type- and function specifications are const...
336 14703 100
New
kip
Image is an image processing library for Elixir. It is based upon the fabulous vix library that provides a libvips wrapper for Elixir. I...
622 19460 194
New
wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130286 1222
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44532 311
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement