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

Where Next?

Popular in Announcing Top

wmnnd
Hi there, for my project DBLSQD, I needed a file storage solution that is a bit more flexible than Arc. Because I thought others might f...
New
tmbb
PhoenixWS - Websockets over Phoenix Channels Source code on Github here: https://github.com/tmbb/phoenix_ws Phoenix channels are a great...
New
martinthenth
Hello everybody :wave: Recently, some of my colleagues talked about database ids and uuids and their problems, and I remembered the pain...
New
devonestes
Introducing assertions, the library that helps you write really great test assertions! GitHub: https://github.com/devonestes/assertions ...
New
mbuhot
Leverage Open Api 3.0 (Swagger) to document, test, validate and explore your Plug and Phoenix APIs. Generate and serve a JSON Open API ...
New
Qqwy
Hello everyone, I wrote a small library today called MapDiff. It returns a map listing the (smallest amount of) changes to get from map...
New
kelvinst
Hey everyone! Well, we made this lib a while ago and now we decided to finally go out and public with it! It’s a tool for creating and m...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. Features Unstyled Phoenix components. Storybook that can be added to...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
kevinlang
Hey all, We have made an Ecto3 Adapter for SQLite3, ecto_sqlite3! We have successfully on-boarded the full suite of integration tests (...
New

Other popular topics Top

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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53578 245
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
nobody
Hi! In PHP: $SERVER['SERVERADDR'] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New

We're in Beta

About us Mission Statement