Live data from Hacker News

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

github.com

1–10 of 31 posts

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

#1
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!

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

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

#2
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.

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

#3

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 metaprog…

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

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

#4
post #3

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 metaprog…

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

the key is an atom called foobar, and the value is being extracted to a variable called foobar. The variable's value is not shown in the example.

You do this quite a bit in Elixir, unless you use the . syntax, e.g. somemap.foobar which is marginally slower.

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

#5
post #3

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 metaprog…

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

This pattern appears frequently in just about all programming languages. I’ve written a lot of JS and Python, and a moderate amount of Elixir, and this pattern crops up quite often. Usually it’s a side effect of complex scopes with several layers of function calls.

Honestly I’m a little surprised you haven’t seen this pattern. The only times it wouldn’t be used are when people rename variables, which is frankly a practice I abhor.

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

#7
post #4
post #3

Earlier quoted context omitted.

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

the key is an atom called foobar, and the value is being extracted to a variable called foobar. The variable's value is not shown in the example. You do this quite a bit in Elixir, unless you use the . syntax, e.g. somemap.foobar which is marginally slower.

Got it - I misunderstood the short code sample, I thought the 2nd half of the tuple was a literal instead of a variable. Makes sense.

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

#8
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...

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

#9
post #3

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 metaprog…

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

The use of : in 'foobar: foobar' here is syntax sugar for

   :foobar, foobar
where :foobar is a symbol that evaluates to itself and foobar evaluates to the value the variable points to.

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

#10
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.

Post reply on HN