Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.



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


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.


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.


JS and python are just about all programming languages?

I'm not trying to be snarky, but e.g. I haven't seen it in Go, F# and Rust, some languages where I have at least seen _some_ code. I also don't remember a similar pattern in Java, C# and C nor PHP or Dart, but I will readily admit that it's I haven't coded in any of these in the last two years, so I might be a bit out of touch and I'm not super proficient in all those languages


Go, and F# do almost all positional arguments. I think Rust too. Java, C#, C, PHP are also positional only or 99% of the time. I assume Dart too.

We are talking named/keyword/etc.

Personally I find positional arguments to be an anti-pattern in all but a very specific set of circumstances.


If I understand correctly, we aren't talking about named/keyword arguments. This is about a shorthand syntax in ES6 objects, nothing to do with function arguments.

In JS, when creating an object, instead of writing {name: name}, where "name" is both the key in the map as well as the variable you want it to assign to, you can do {name}, whereas e.g. in Go you would do map[string]any{"name": name}.

Edit: Or, for completeness' sake, it's the same with structs in Go, where you would instantiate a struct like this: Person{name: name}.


C#, F# and PHP all support named arguments and they're quite common for optional parameters.


If we're talking about constructing structs, like in `Foo { bar, baz: 123 }` (with the `bar` style shortcut in it), I have used that kind of syntax 10 times in 16 KLOC of Rust. Not a lot, but it does happen, and I found it kinda neat when LSP integration suggested its use.

I've probably used it more for pattern matching (`let Foo { bar, baz } = ...`), but haven't measured the number of instances of that idiom.


You're right constructing struts in Rust works with that pattern, so I'll take the Rust bit back.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: