top | item 40333500

Show HN: es6_maps, new Elixir syntax feature via runtime compiler hacking

71 points| kzemek | 1 year ago |github.com

Hi HN! I thought you might enjoy this even if you don't know much about Elixir.

`es6_maps` is a small library that introduces a "shorthand" map creation syntax, similar to shorthand object construction in JavaScript/TypeScript. For those unfamiliar with Elixir, the most interesting aspect might be how it achieves this. `es6_maps` takes advantage of BEAM (Erlang's VM) hot-reload capabilities to amend the Elixir compiler bytecode at runtime, adding functions that expand the shorthand syntax for further compilation. I think it's a nice showcase of the power you can wield (with care) when writing in BEAM languages.

Let me know what you think!

30 comments

order

jzimbel|1 year ago

This was done previously with the `short_maps` library, which the author—a member of the Elixir core team—eventually retired and archived due to regrets from the undesirable impacts on clarity.

https://github.com/whatyouhide/short_maps https://andrealeopardi.com/posts/a-story-of-regret-and-retir...

kzemek|1 year ago

There was also a relatively popular fork shorter_maps [0], mentioned in the blog post above. My motivation for implementing es6_maps instead of using/updating shorter_maps was (I'm copying from my post in the Elixir forums [1]):

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.

[0] https://github.com/meyercm/shorter_map [1] https://elixirforum.com/t/es6-maps-ecmascript6-style-shortha...

MrJohz|1 year ago

That's quite surprising to me. As someone who writes a lot of Javascript, I usually find it easiest to read with this syntax than without it, and I generally recommend to my team that they use the shorthand where they can. This is because the meaning is clear and well-known, and being concise (while still being clear) is very valuable.

I wonder what specifically the original author found made it more difficult to read. Was it unfamiliarity? They allude to some confusion between sigils and strings, so I wonder if the issue was partly an Elixir-specific one.

aarongraham|1 year ago

I really don’t understand why some people are so against this as a language feature. I get that the community tries to stay away from “magic” but where is the line between magic and convenient syntactic sugar.

For example keyword lists as the last argument to a function don’t need brackets, and keyword lists are really lists of tuples where the first item is an atom. I feel like those two things are in some ways less obvious and more magical than this map shorthand.

bryantwolf|1 year ago

I've been using Destucture [1] for a few years. It's an interesting compromise that works for atom and string keys because it's explicit, kind of like the sigil approach. Though this seems way more streamlined, especially as someone who uses JS for front-end apps.

[1]https://github.com/danielberkompas/destructure/

h0l0cube|1 year ago

I like this because it makes the sugaring distinct. Anyone unfamiliar with the sugar will know there's something different going on, while not really adding much noise.

rubyn00bie|1 year ago

I personally don’t think this is a good idea, and I think the formatter plugin is going to give someone insane amounts of regret one day. It’s neat as “look what I did” but Elixir is all about removing magic.

It (lack of magic) is one of the things I appreciated most, having originally come from Ruby and Rails. I’ve been using it professionally for nearly ten years, and I can say this isn’t a feature I’ve ever really wanted. My text editor can save the typing via shortcuts I type, and in doing so supports string keys, atom keys, has zero compile time dependencies, and no new syntax ;)

I’d really encourage you to put a note at the top of the readme that this shouldn’t be used in production code.

kzemek|1 year ago

> I think the formatter plugin is going to give someone insane amounts of regret one day

The formatter plugin features a "reverse" flag exactly to prevent any kind of regret like that. You can reformat your code automatically to remove all shorthand maps and remove the dependency in a couple simple steps.

> I’d really encourage you to put a note at the top of the readme that this shouldn’t be used in production code.

Noted, although I do think it's production ready. I'm going to keep updating the library if or when a new version of the Elixir compiler breaks it, and it does feature an easy way out in case I disappear.

sb8244|1 year ago

I don't agree that this is magic. It's only "magic" because the language doesn't do it today (thus requiring the compiler extension.) If this were in the language itself, it would probably be pretty popular.

The argument against it feels like "because we've always done it this way."

karmajunkie|1 year ago

i don’t like adding foundational syntax using libraries, but i do wish they would add this to the language. deconstruction by pattern is super common and the syntactic sugar would be most welcome.

arrowsmith|1 year ago

This is great!

I've always thought that if I could change one thing about Elixir, it would be to add exactly this shorthand syntax that I'm used to from JS. I'm sick of having to type everything twice, like e.g. `%{foobar: foobar}`.

I'm impressed to see it implemented. I didn't know it was possible to pass custom compilers to your Mix project definition - but now that I think about it, I can see how Elixir's metaprogramming features would lend themselves well to implementing custom syntax like this.

Fantastic work, all I want to know is why Elixir doesn't have this syntax built-in already.

dudul|1 year ago

> I'm sick of having to type everything twice, like e.g. `%{foobar: foobar}`

I'm actually very confused by this use case. How often do you have to do that? Fill a map with kv tuples where keys and values have the exact same value?

vessenes|1 year ago

A good reminder that macros give you super powers!

I don't know a lot of elixir, so I can't say if this is a great idea for inclusion into the standard or not, but it's undeniable that giving macro-based access to the AST adds a lot of expressivity and flexibility.

cmoski|1 year ago

Does it work for nested maps?

kzemek|1 year ago

Yes it does!