This syntax sugar is a nightmare to me. Very confusing and for what? To save 2 keystrokes?
This syntax sugar is a nightmare to me. Very confusing and for what? To save 2 keystrokes?
"foo(arg, my_keyword1: my_keyword1, my_keyword2: my_keyword2, my_keyword3: my_keyword3)"
that now becomes:
"foo(arg, my_keyword1:, my_keyword2:, my_keyword3:)"
that is just a bit less for your eyes to have to digest and parse, although i can see leaving the trailing colon off as being a common bug.
sometimes you can just double splat both the signature and the wrapped invocation, but if you need to peel off kwargs in the wrapper method then you're stuck passing them all back in, or else you fall back to pulling kwargs out of a hash (plus you lose some level of auto-documentation in the method signature that way).
it'd be kind of nice to have a literal which could be passed into the method invocation that would expand out to all the kwargs that were passed, while still being explicit in the definition.
Also, how is this going to work in function heads? `def foo(bar:)` means that `bar` is a required keyword. Is it going to have to be `def foo({bar:})`? In that case, it's going to come off as a potential style error to many people.
def some_method(with_longer_parameters: with_longer_parameters)
do_something(with_longer_parameters)
end
vs def some_method(with_longer_parameters:)
do_something(with_longer_parameters)
endYour second definition reads that `with_longer_parameters` is a keyword argument with no default.
Still, you've illustrated the keystroke savings with your choice of parameter names :-)
def login(username: ENV["USER"], password:)
p(username:, password:)
end
login(password: "xxx") #=> {:username=>"shugo", :password=>"xxx"}That way - if you're new to a language and don't want to deal with all the cognitive overhead of syntactic sugar (or if you just find some particular feature really complicated) - you could "un-see" it.
A lot more keystrokes if you are using meaningful argument and/or hash key names, but the big benefit is, IMO, readability (at least, that's the big benefit I see with the very similar syntax in JS.)
If you want to save keystrokes, Ruby is probably not your language. And you also really need to invest some time in your editor/IDE.
The sugar is that it removes redundancy and adds DRY:
> Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
Now, the value for DRY is minimal here: `referer_acceptence_header: referrer_acceptance_header` is quite easy to spot (and there too: if these are your bugs, you probably need to invest some time in your editor/IDE), but avoiding it alltogether in the language is always nice.