[ruby-core:117631] [Ruby master Misc#20441] Should passing keyword args to method_name(*) be an error?

Issue #20441 has been reported by ozydingo (Andrew Schwartz). ---------------------------------------- Misc #20441: Should passing keyword args to method_name(*) be an error? https://bugs.ruby-lang.org/issues/20441 * Author: ozydingo (Andrew Schwartz) * Status: Open ---------------------------------------- In the following method: ```rb def foo(*) super end ``` it is apparently the intended ruby 3 behavior to pass keyword args as a positional Hash to `super`. I believe this is confusing and can lead to hidden and hard-to-discover bugs (e.g. #20440). Since `*` is meant to only represent positional args, should it be an ArgumentError to pass keyword args at all to this method? Similar to how it is an error to pass positions args to `bar(**)`. -- https://bugs.ruby-lang.org/

Issue #20441 has been updated by zverok (Victor Shepelev). `super` just passes the arguments with EXACTLY the same signature as the method it is in has. Whether or not `super` is in the method, calling method defined as `foo(*)` with hash-like arguments without braced will implicitly convert them to the Hash as the last positional argument, and it is unlikely to change. What exactly has `super` to do with it?.. ---------------------------------------- Misc #20441: Should passing keyword args to method_name(*) be an error? https://bugs.ruby-lang.org/issues/20441#change-108040 * Author: ozydingo (Andrew Schwartz) * Status: Open ---------------------------------------- In the following method: ```rb def foo(*) super end ``` it is apparently the intended ruby 3 behavior to pass keyword args as a positional Hash to `super`. I believe this is confusing and can lead to hidden and hard-to-discover bugs (e.g. #20440). Since `*` is meant to only represent positional args, should it be an ArgumentError to pass keyword args at all to this method? Similar to how it is an error to pass positions args to `bar(**)`. -- https://bugs.ruby-lang.org/

Issue #20441 has been updated by ozydingo (Andrew Schwartz). Why does this conversion to a Hash occur? I would guess for some sense of backward compatibility with gems / code written in earlier versions of Ruby. But #20440 demonstrates why this compatibility is not achieved. To be clear, I'm not arguing it _should_ be backward compatible, and it isn't; but they why should `*` convert keyword args to a Hash instead of considering it an error? `super` only comes into play because that's the only time you'll silently pass the converted arg in code that might not be compatible with doing so, such as in the linked example. Without `super` the args are simply unused. ---------------------------------------- Misc #20441: Should passing keyword args to method_name(*) be an error? https://bugs.ruby-lang.org/issues/20441#change-108041 * Author: ozydingo (Andrew Schwartz) * Status: Open ---------------------------------------- In the following method: ```rb def foo(*) super end ``` it is apparently the intended ruby 3 behavior to pass keyword args as a positional Hash to `super`. I believe this is confusing and can lead to hidden and hard-to-discover bugs (e.g. #20440). Since `*` is meant to only represent positional args, should it be an ArgumentError to pass keyword args at all to this method? Similar to how it is an error to pass positions args to `bar(**)`. -- https://bugs.ruby-lang.org/

Issue #20441 has been updated by zverok (Victor Shepelev).
Why does this conversion to a Hash occur?
Because of backward compatibility, indeed. Even now, most of, say, Rails code still uses “old” conventions: ```ruby def foo(arg1, arg2, options = {}) # ... end # and expects it to be called as foo("bar", 1, opt1: val1, opt2: val3) ``` Prohibiting this (and requiring to pass to such methods only explicit hashes like `foo("bar", 1, {opt1: val1, opt2: val3})`) would break an enormous amount of code. So, the last hash-like pack of arguments would be treated as keyword args for methods with them in the signature and as a last positional hash for methods without them. The question of “generic delegation” in such conditions (with `super` or otherwise) is a subtle one, but it is mostly solved and requires following only two rules, as was [explained](https://bugs.ruby-lang.org/issues/20440#note-3) in the previous ticket: * For Ruby 3+, to “delegate all possible kinds of arguments”, use `foo(*, **)` * If the code needs to be compatible with Ruby < 3, use `ruby2_keywords` If we do neither, the method defined as `foo(*)` wouldn’t try to guess how to convert its arguments back to positional+keyword on `super` or other forms of delegation and would simply consider them all positional. ---------------------------------------- Misc #20441: Should passing keyword args to method_name(*) be an error? https://bugs.ruby-lang.org/issues/20441#change-108042 * Author: ozydingo (Andrew Schwartz) * Status: Open ---------------------------------------- In the following method: ```rb def foo(*) super end ``` it is apparently the intended ruby 3 behavior to pass keyword args as a positional Hash to `super`. I believe this is confusing and can lead to hidden and hard-to-discover bugs (e.g. #20440). Since `*` is meant to only represent positional args, should it be an ArgumentError to pass keyword args at all to this method? Similar to how it is an error to pass positions args to `bar(**)`. -- https://bugs.ruby-lang.org/

Issue #20441 has been updated by ozydingo (Andrew Schwartz). Understanding better the role of `ruby2_keywords` is helping, thank you. It seemed to me that either way some compatibility was broken, but the subtleties of maintaining compatibility as well as possible in a variety of circumstances is indeed tricky. I appreciate your time explaining it further here. ---------------------------------------- Misc #20441: Should passing keyword args to method_name(*) be an error? https://bugs.ruby-lang.org/issues/20441#change-108043 * Author: ozydingo (Andrew Schwartz) * Status: Open ---------------------------------------- In the following method: ```rb def foo(*) super end ``` it is apparently the intended ruby 3 behavior to pass keyword args as a positional Hash to `super`. I believe this is confusing and can lead to hidden and hard-to-discover bugs (e.g. #20440). Since `*` is meant to only represent positional args, should it be an ArgumentError to pass keyword args at all to this method? Similar to how it is an error to pass positions args to `bar(**)`. -- https://bugs.ruby-lang.org/
participants (2)
-
ozydingo (Andrew Schwartz)
-
zverok (Victor Shepelev)