by burlesona · 4y ago · 0 comments
> Values in Hash literals and keyword arguments can be omitted. [Feature #14579]
> {x:, y:} is a syntax sugar of {x: x, y: y}.
> foo(x:, y:) is a syntax sugar of foo(x: x, y: y).
This is going to be controversial and take some getting used to, but I think eventually everyone will love it. It’s not an easy syntax to learn - I remember when this came to JS it took a while to get my head around all the different ways this shorthand was used in the wild.
It’s hard to come up with new syntax that can peacefully coexist with the established language. I think in hindsight it would have been helpful for Ruby if it had done something like square braces instead of brackets for hash literals, or not allowed brackets for blocks. The most ambiguous syntax all involves hash literals vs blocks… this adds to that cognitive load.
But typing out a long hash literal just to capture a bunch of local variables or methods is so common, and the duplication is quite annoying, so on the whole I’m glad this was added and am sure I’ll use it a lot :)
I'm a little disappointed that Ruby introduced it but no one is forcing me to use it so to each their own (and I'm not writing Ruby or much JS professionally anymore so it doesn't affect me too much).
It's saving a single character in your example. I can see liking it as solving a minor inconvenience. But love? I don't see that. To me it's just another thing to learn.
{width:, height:, x_pos:, y_pos:}
to be much more pleasant than
{width: width, height: height, x_pos: x_pos, y_pos: y_pos}
And the more items you add the more I find it useful. Yeah, it took some getting used to in Javascript after years and years without it, but I personally would not want to go back to not having it.
I'm not crazy about the colon/comma combo, but if I was writing in Ruby I suspect I'd get used to it after a while.
Arguably it was the right call considering where Ruby is now.
foo x:
[1,2,3]
Or maybe that "punning" will work only inside of () and {}?See also https://zverok.github.io/blog/2021-12-08-value-omission-debu...