ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: merch-redmine@jeremyevans.net
To: ruby-core@ruby-lang.org
Subject: [ruby-core:90240] [Ruby trunk Feature#14183] "Real" keyword argument
Date: Mon, 03 Dec 2018 03:40:17 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-75356.20181203034015.71620998d4cc41c8@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-14183.20171214065906@ruby-lang.org

Issue #14183 has been updated by jeremyevans0 (Jeremy Evans).


mame (Yusuke Endoh) wrote:
> And, this is a new topic.  There is an non-Symbol-key call, like `where("table.id" => 1)` (which was shown by Jeremy Evans).  This is difficult to change to keyword argument.  To allow this, matz came up with an idea: non-Symbol key is also allowed as a keyword.
> 
> ```
> def foo(**kw)
>   p kw
> end
> 
> foo("str" => 42) #=> {"str"=>42}
> ```
> 
> Note that, if you need to write a library that works on both 2.X and 3.X, you must write a shim:
> 
> ```
> def foo(kw1 = {}, **kw2)
>   kw = kw1.merge(kw2)
>   kw
> end
> ```
> 
> However, after EOL of all Ruby 2.X series, you can remove the shim and just write a simple code.  This is better than my original proposal.
> 
> What do you think?

I agree with the proposed changes to `foo`-like (keyword arguments) methods, as those changes actually solve real problems with keyword arguments.

I disagree with the proposed changes to `bar`-like (positional hash arguments) methods. Those changes do not solve existing problems, they just break existing code for the potential future ability to introduce keyword arguments without behavior changes.

Let's consider if this proposed changes to `bar`-like (positional hash arguments) methods is accepted.  The main argument for acceptance is the ability to introduce keyword arguments without behavior changes.  However, in many if not most cases where `bar`-like methods are used, keyword arguments would be used to replace the option hashes (something that works OK in 2.X except for corner cases with optional position arguments and argument splats), not as an addition to option hashes.  With the changes discussed to `foo`-like methods, you would no longer be able to replace an option hash argument with keyword arguments in a backwards compatible manner.  So the fact that the proposed changes to `bar`-like methods allow keyword arguments to be introduced in a backwards compatible manner will not help, since replacing the option hashes with keywords will still be a backwards incompatible change.

There are many cases where you have a method that accepts a hash where you have cases where you want to pass an existing hash and other cases where you want pass a new hash, and having to add braces to all call-sites where you currently can omit them would be annoying, add no value, and make the code slightly harder to read.

The performance disadvantages to keyword splats that I discussed earlier still have not be addressed, and it is still impossible to create a method that accepts arbitrary keyword arguments and delegates the call to another method that accepts arbitrary keyword arguments without at least 3 hash allocations per-call (and you can have 0 hash allocations per call with a option-hash based approach).

In conclusion, the ability to add keyword arguments in a backwards compatible manner to methods that accept option hashes adds very little benefit. I think there are huge costs in breaking existing compatibility (potentially leading to a Python 2/3-like situation in libraries), and other costs in making code using `bar`-like methods harder to read (by requiring braces), as well as hurting performance by encouraging unoptimized keyword splats as a replacement for option hashes.

The ability for `**kw` to accept non-Symbol keys would make it a slightly easier to convert option hash methods to keyword arguments methods, but the keyword argument approach would still perform worse due to the additional hash allocations, and converting option hashes to keyword splats would still not be backwards compatible, and I think in most cases using an options hash would still be the preferable approach.  So even if `**kw` handled non-Symbol keys, I would still be strongly against changing the behavior for `bar`-like methods.

----------------------------------------
Feature #14183: "Real" keyword argument
https://bugs.ruby-lang.org/issues/14183#change-75356

* Author: mame (Yusuke Endoh)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: Next Major
----------------------------------------
In RubyWorld Conference 2017 and RubyConf 2017, Matz officially said that Ruby 3.0 will have "real" keyword arguments.  AFAIK there is no ticket about it, so I'm creating this (based on my understanding).

In Ruby 2, the keyword argument is a normal argument that is a Hash object (whose keys are all symbols) and is passed as the last argument.  This design is chosen because of compatibility, but it is fairly complex, and has been a source of many corner cases where the behavior is not intuitive.  (Some related tickets: #8040, #8316, #9898, #10856, #11236, #11967, #12104, #12717, #12821, #13336, #13647, #14130)

In Ruby 3, a keyword argument will be completely separated from normal arguments.  (Like a block parameter that is also completely separated from normal arguments.)
This change will break compatibility; if you want to pass or accept keyword argument, you always need to use bare `sym: val` or double-splat `**` syntax:

```
# The following calls pass keyword arguments
foo(..., key: val)
foo(..., **hsh)
foo(..., key: val, **hsh)

# The following calls pass **normal** arguments
foo(..., {key: val})
foo(..., hsh)
foo(..., {key: val, **hsh})

# The following method definitions accept keyword argument
def foo(..., key: val)
end
def foo(..., **hsh)
end

# The following method definitions accept **normal** argument
def foo(..., hsh)
end
```

In other words, the following programs WILL NOT work:

```
# This will cause an ArgumentError because the method foo does not accept keyword argument
def foo(a, b, c, hsh)
  p hsh[:key]
end
foo(1, 2, 3, key: 42)

# The following will work; you need to use keyword rest operator explicitly
def foo(a, b, c, **hsh)
  p hsh[:key]
end
foo(1, 2, 3, key: 42)

# This will cause an ArgumentError because the method call does not pass keyword argument
def foo(a, b, c, key: 1)
end
h = {key: 42}
foo(1, 2, 3, h)

# The following will work; you need to use keyword rest operator explicitly
def foo(a, b, c, key: 1)
end
h = {key: 42}
foo(1, 2, 3, **h)
```

I think here is a transition path:

* Ruby 2.6 (or 2.7?) will output a warning when a normal argument is interpreted as keyword argument, or vice versa.
* Ruby 3.0 will use the new semantics.



-- 
https://bugs.ruby-lang.org/

  parent reply	other threads:[~2018-12-03  3:40 UTC|newest]

Thread overview: 90+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-14183.20171214065906@ruby-lang.org>
2017-12-14  6:59 ` [ruby-core:84255] [Ruby trunk Bug#14183] "Real" keyword argument mame
2017-12-14  8:27 ` [ruby-core:84258] " merch-redmine
2018-01-16 17:33 ` [ruby-core:84894] [Ruby trunk Feature#14183] " mail
2018-01-19  3:10 ` [ruby-core:84929] " danieldasilvaferreira
2018-07-23  1:06 ` [ruby-core:88052] " mame
2018-07-23  1:20 ` [ruby-core:88053] " mame
2018-07-23  1:49 ` [ruby-core:88055] " merch-redmine
2018-07-23  3:10 ` [ruby-core:88058] " mame
2018-07-24 23:04 ` [ruby-core:88086] " merch-redmine
2018-07-26 10:38 ` [ruby-core:88123] " shevegen
2018-07-26 15:32 ` [ruby-core:88126] " matz
2018-08-30 23:10 ` [ruby-core:88765] " merch-redmine
2018-08-31  1:56 ` [ruby-core:88766] " mame
2018-08-31  5:20 ` [ruby-core:88771] " merch-redmine
2018-08-31  5:45 ` [ruby-core:88772] " mame
2018-08-31  8:05 ` [ruby-core:88779] " mame
2018-08-31 14:42 ` [ruby-core:88786] " merch-redmine
2018-09-01  1:32 ` [ruby-core:88793] " mame
2018-09-03  2:35 ` [ruby-core:88813] " duerst
2018-09-04 22:46 ` [ruby-core:88839] " eregontp
2018-09-05  3:01 ` [ruby-core:88843] " mame
2018-09-05  6:47 ` [ruby-core:88851] " mame
2018-09-05 16:59 ` [ruby-core:88869] " ruby-core
2018-09-05 17:05 ` [ruby-core:88870] " ruby-core
2018-09-09  3:33 ` [ruby-core:88908] " ruby-core
2018-09-17  5:22 ` [ruby-core:89037] " akr
2018-09-17 14:56 ` [ruby-core:89042] " merch-redmine
2018-09-18  0:00 ` [ruby-core:89047] " akr
2018-09-18  3:44 ` [ruby-core:89052] " ruby-core
2018-12-03  2:31 ` [ruby-core:90237] " mame
2018-12-03  3:40 ` merch-redmine [this message]
2018-12-22 11:41 ` [ruby-core:90672] " fg
2018-12-28  0:01 ` [ruby-core:90758] " samuel
2019-03-18  9:21 ` [ruby-core:91864] " mame
2019-03-18 19:22 ` [ruby-core:91873] " merch-redmine
2019-03-25 22:50 ` [ruby-core:91986] " merch-redmine
2019-03-29 10:31 ` [ruby-core:92049] " mame
2019-03-29 14:57 ` [ruby-core:92050] " merch-redmine
2019-04-01 14:52 ` [ruby-core:92093] " matz
2019-04-01 23:09 ` [ruby-core:92095] " merch-redmine
2019-04-08  2:29 ` [ruby-core:92192] " merch-redmine
2019-04-09  3:20 ` [ruby-core:92217] " merch-redmine
2019-04-12  8:26 ` [ruby-core:92255] " mame
2019-04-12 13:52 ` [ruby-core:92257] " mame
2019-04-12 15:34 ` [ruby-core:92258] " merch-redmine
2019-04-17  5:05 ` [ruby-core:92312] " eregontp
2019-04-19 13:32 ` [ruby-core:92330] " merch-redmine
2019-04-24 20:03 ` [ruby-core:92404] " merch-redmine
2019-07-04 11:05 ` [ruby-core:93541] [Ruby master " sawadatsuyoshi
2019-07-08  1:58 ` [ruby-core:93606] " sawadatsuyoshi
2019-07-08  2:35 ` [ruby-core:93608] " merch-redmine
2019-08-16  6:52 ` [ruby-core:94379] " mame
2019-08-16  6:55 ` [ruby-core:94380] " mame
2019-08-16 14:36 ` [ruby-core:94388] " merch-redmine
2019-08-16 23:05 ` [ruby-core:94392] " merch-redmine
2019-08-17  0:33 ` [ruby-core:94394] " mame
2019-08-17  1:44 ` [ruby-core:94395] " merch-redmine
2019-08-20 19:40 ` [ruby-core:94448] " merch-redmine
2019-08-29  3:26 ` [ruby-core:94637] " daniel
2019-08-29  3:53 ` [ruby-core:94638] " merch-redmine
2019-08-30 21:11 ` [ruby-core:94691] " merch-redmine
2019-08-31  3:06 ` [ruby-core:94698] " daniel
2019-09-02 21:43 ` [ruby-core:94748] " merch-redmine
2019-09-04 17:20 ` [ruby-core:94779] " daniel
2019-09-04 23:51 ` [ruby-core:94780] " merch-redmine
2019-09-05  1:54 ` [ruby-core:94782] " daniel
2019-09-05  1:57 ` [ruby-core:94783] " daniel
2019-10-23 17:59 ` [ruby-core:95509] " bughitgithub
2019-11-08  7:01 ` [ruby-core:95754] " sam.saffron
2019-11-08 10:14 ` [ruby-core:95756] " mame
2019-11-08 13:50 ` [ruby-core:95757] " daniel
2019-11-08 14:25 ` [ruby-core:95758] " mame
2019-12-03 10:02 ` [ruby-core:96069] " mame
2019-12-03 23:45 ` [ruby-core:96092] " merch-redmine
2019-12-06 21:13 ` [ruby-core:96132] " koic.ito
2019-12-06 21:26 ` [ruby-core:96133] " merch-redmine
2019-12-06 22:44 ` [ruby-core:96134] " zverok.offline
2019-12-06 22:59 ` [ruby-core:96135] " merch-redmine
2019-12-06 23:14 ` [ruby-core:96136] " zverok.offline
2019-12-07  0:19 ` [ruby-core:96137] " merch-redmine
2019-12-08 11:22 ` [ruby-core:96143] " zverok.offline
2019-12-08 22:26 ` [ruby-core:96145] " fg
2019-12-09  3:47 ` [ruby-core:96149] " daniel
2019-12-11  9:25 ` [ruby-core:96195] " koic.ito
2019-12-11 14:16 ` [ruby-core:96201] " daniel
2019-12-11 15:29 ` [ruby-core:96206] " merch-redmine
2019-12-13 17:08 ` [ruby-core:96219] " bughitgithub
2019-12-13 17:45 ` [ruby-core:96220] " merch-redmine
2019-12-13 19:58 ` [ruby-core:96222] " bughitgithub
2019-12-20  7:50 ` [ruby-core:96371] " matz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-75356.20181203034015.71620998d4cc41c8@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).