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
Hex: es6_maps | Hex
HexDocs: es6_maps v1.0.1 — Documentation

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: 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

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.net/t/proposal-add-field-puns-map-shorthand-to-elixir/15452

kzemek

kzemek

Is there a reason why you have created a custom mix task for formatting instead of writing a plugin for .formatter.exs file?

A couple:

  • It’s very hard to pass-through formatting when implementing Mix.Tasks.Format behaviour. If your plugin says it handles [".ex", ".exs"] extensions, it will take over these extensions completely, and the original formatter won’t run anymore.
    In its initial incarnation the formatter only concerned itself with maps, leaving the rest of the formatting up to the default formatter. It’s true that the current version basically does the whole format though, calling Mix.Format just in case…
  • But more importantly, I included a formatting task as a way to jump all-in, but primarily as a guarantee for people that they’ll be able to jump out if needed. I did see that as a one-off, explicitly called task.

That said, with the current implementation it might make sense to both have the task and an optional formatter for people to use, sharing the same code.

Could you please add some kind of verification to the compile task (with special form maybe?), so the code would not fail in said task and it would automatically call --revert and print a warning instead? Is it possible in this case?

It is possible, but I don’t think I can sensibly guard / detect against incompatible changes in the compiler without knowing what the changes would be. Also note that es6_maps only modifies the compiler that runs in a separate pass, it doesn’t compile things by itself.

To be clear, if I could detect that the code would fail to compile due to the shorthand-form maps I wouldn’t reformat user’s on-disk files anyway – and, if I found a good way to modify AST and pass it to the compiler, I would have rewritten the whole library to use that mechanism instead and there wouldn’t be a need for a fallback in the first place.

kzemek

kzemek

Is there a reason why you have created a custom mix task for formatting instead of writing a plugin for .formatter.exs file?

Following our discussion, I have just released v0.2.2 where the custom format task is replaced with a formatting plugin that can be additionally configured inline with comment pragmas.

Where Next?

Popular in Libraries Top

josevalim
Hi everyone, We would like to announce that Plataformatec is working on a new MySQL driver called MyXQL. Our goal is to eventually integ...
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...
574 16576 179
New
nikokozak
Hello all, I’ve been working on Svonix - a library for quickly integrating Svelte components into Phoenix views. It’s a much-needed succ...
New
arkgil
Hi all! I’m happy to announce that Telemetry v0.3.0 is out! This release marks the conversion from Elixir to Erlang so that all the libr...
New
New
benlime
I created a new library GitHub - benvp/ex_cva: Class Variance Authority for Elixir which aims to make it very easy to define different va...
New
Antrater
Hi everyone! I’m thrilled to announce a huge thing. We have been developing Elixir Moon Design System for quite a while. We are finally ...
New
bryanjos
Hi, I wanted share a small library we at Revelry Labs made for rendering react components from the server side. There are instructions fo...
New
mattludwigs
Grizzly is a library for working with Z-Wave devices. Z-Wave is a low-frequency radio protocol for controlling smart home devices on a me...
New
bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
381 12391 119
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
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 27727 240
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

Sub Categories:

We're in Beta

About us Mission Statement