kzemek
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 · GitHub
Hex: es6_maps | Hex
HexDocs: es6_maps v1.0.3 — Documentation
Trending in Announcing
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Eiji
Is there a reason why you have created a custom
mixtask for formatting instead of writing a plugin for.formatter.exsfile?Could you please add some kind of verification to the compile task (
withspecial form maybe?), so the code would not fail in said task and it would automatically call--revertand print a warning instead? Is it possible in this case?kzemek
A couple:
Mix.Tasks.Formatbehaviour. 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.Formatjust in case…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.
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_mapsonly 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.
rogach
Will LSP server (e.g. elixir-ls) work with this library?
slouchpie
kzemek
I’m using ElixirLS, and while it doesn’t pick up e.g.
helloin%{hello} = %{hello: 1}as a new variable for autocomplete, it also doesn’t throw up any warnings or errors to the user - because the syntax is correct and the code compiles. It does however complain about the line in its internal warning output.Getting more support in an LS is an interesting point of improvement. I wonder if I could influence it from the “outside” similarly to how I do the compiler.
kzemek
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.
rogach
I looked at the implementation, and I must say that I’m impressed by the flexibility of Elixir and BEAM. It’s wicked, but it’s good to know that you can modify the behavior this deeply.
These shorthand map keys look extremely useful to me personally, I find that my map keys and variable names are often the same. Are there any prior discussions about making this shorthand notation part of “standard” compiler? Would be interesting to know why developers decided against it.
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://forum.elixirforum.com/t/proposal-add-field-puns-map-shorthand-to-elixir/15452
blatyo
FWIW, I think this feature could likely get accepted into Elixir. The main problem IMO is that most people want bare words, but Jose wants there to be a visible distinction so it’s clear what the key type is. I think if someone proposed the ruby style syntax for this instead and was willing to do the work to implement it, it’d be accepted.
Basically, instead of:
do:
I don’t see anywhere in your examples anything about the pin operator (^), map update syntax
%{x | foo: 1}, structs%Foo{bar: 1}, or struct update syntax%Foo{baz | bar: 1}. If they’re not supported, I’d argue they should be added.