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