ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:117631] [Ruby master Misc#20441] Should passing keyword args to method_name(*) be an error?
@ 2024-04-21  8:25 ozydingo (Andrew Schwartz) via ruby-core
  2024-04-21  8:33 ` [ruby-core:117632] " zverok (Victor Shepelev) via ruby-core
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: ozydingo (Andrew Schwartz) via ruby-core @ 2024-04-21  8:25 UTC (permalink / raw)
  To: ruby-core; +Cc: ozydingo (Andrew Schwartz)

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/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:117632] [Ruby master Misc#20441] Should passing keyword args to method_name(*) be an error?
  2024-04-21  8:25 [ruby-core:117631] [Ruby master Misc#20441] Should passing keyword args to method_name(*) be an error? ozydingo (Andrew Schwartz) via ruby-core
@ 2024-04-21  8:33 ` zverok (Victor Shepelev) via ruby-core
  2024-04-21  8:47 ` [ruby-core:117633] " ozydingo (Andrew Schwartz) via ruby-core
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: zverok (Victor Shepelev) via ruby-core @ 2024-04-21  8:33 UTC (permalink / raw)
  To: ruby-core; +Cc: zverok (Victor Shepelev)

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/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:117633] [Ruby master Misc#20441] Should passing keyword args to method_name(*) be an error?
  2024-04-21  8:25 [ruby-core:117631] [Ruby master Misc#20441] Should passing keyword args to method_name(*) be an error? ozydingo (Andrew Schwartz) via ruby-core
  2024-04-21  8:33 ` [ruby-core:117632] " zverok (Victor Shepelev) via ruby-core
@ 2024-04-21  8:47 ` ozydingo (Andrew Schwartz) via ruby-core
  2024-04-21  8:58 ` [ruby-core:117634] " zverok (Victor Shepelev) via ruby-core
  2024-04-21 14:33 ` [ruby-core:117635] " ozydingo (Andrew Schwartz) via ruby-core
  3 siblings, 0 replies; 5+ messages in thread
From: ozydingo (Andrew Schwartz) via ruby-core @ 2024-04-21  8:47 UTC (permalink / raw)
  To: ruby-core; +Cc: ozydingo (Andrew Schwartz)

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/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:117634] [Ruby master Misc#20441] Should passing keyword args to method_name(*) be an error?
  2024-04-21  8:25 [ruby-core:117631] [Ruby master Misc#20441] Should passing keyword args to method_name(*) be an error? ozydingo (Andrew Schwartz) via ruby-core
  2024-04-21  8:33 ` [ruby-core:117632] " zverok (Victor Shepelev) via ruby-core
  2024-04-21  8:47 ` [ruby-core:117633] " ozydingo (Andrew Schwartz) via ruby-core
@ 2024-04-21  8:58 ` zverok (Victor Shepelev) via ruby-core
  2024-04-21 14:33 ` [ruby-core:117635] " ozydingo (Andrew Schwartz) via ruby-core
  3 siblings, 0 replies; 5+ messages in thread
From: zverok (Victor Shepelev) via ruby-core @ 2024-04-21  8:58 UTC (permalink / raw)
  To: ruby-core; +Cc: zverok (Victor Shepelev)

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/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:117635] [Ruby master Misc#20441] Should passing keyword args to method_name(*) be an error?
  2024-04-21  8:25 [ruby-core:117631] [Ruby master Misc#20441] Should passing keyword args to method_name(*) be an error? ozydingo (Andrew Schwartz) via ruby-core
                   ` (2 preceding siblings ...)
  2024-04-21  8:58 ` [ruby-core:117634] " zverok (Victor Shepelev) via ruby-core
@ 2024-04-21 14:33 ` ozydingo (Andrew Schwartz) via ruby-core
  3 siblings, 0 replies; 5+ messages in thread
From: ozydingo (Andrew Schwartz) via ruby-core @ 2024-04-21 14:33 UTC (permalink / raw)
  To: ruby-core; +Cc: ozydingo (Andrew Schwartz)

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/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-04-21 14:34 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-21  8:25 [ruby-core:117631] [Ruby master Misc#20441] Should passing keyword args to method_name(*) be an error? ozydingo (Andrew Schwartz) via ruby-core
2024-04-21  8:33 ` [ruby-core:117632] " zverok (Victor Shepelev) via ruby-core
2024-04-21  8:47 ` [ruby-core:117633] " ozydingo (Andrew Schwartz) via ruby-core
2024-04-21  8:58 ` [ruby-core:117634] " zverok (Victor Shepelev) via ruby-core
2024-04-21 14:33 ` [ruby-core:117635] " ozydingo (Andrew Schwartz) via ruby-core

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).