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:92192] [Ruby trunk Feature#14183] "Real" keyword argument
Date: Mon, 08 Apr 2019 02:29:48 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-77517.20190408022948.ee2f23ed4f9aab82@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-14183.20171214065906@ruby-lang.org

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


I have updated my GitHub branch (https://github.com/jeremyevans/ruby/commits/keyword-argument-separation) to fix the issues in mame's branch that I identified in my previous comment.

Now, my branch keeps compatibility with Ruby 2.6 in regards to treating a hash argument as keywords, issuing warnings as expected for all three cases where behavior will change in Ruby 3:

```ruby
def foo(a, *b, **c)
  [a, b, c]
end

def bar(a, b=1, **c)
  [a, b, c]
end

def baz(a, **c)
  [a, c]
end

foo(1, {a: 1})
# warning: The last argument for `foo' (defined at (irb):1) is used as the keyword parameter
# => [1, [], {:a=>1}]

bar(1, {a: 1})
# warning: The last argument for `bar' (defined at (irb):5) is used as the keyword parameter
# => [1, 1, {:a=>1}]

baz(1, {a: 1})
# warning: The last argument for `baz' (defined at (irb):9) is used as the keyword parameter
# => [1, {:a=>1}]
```

I have also found another behavior change with mame's branch that I think is undesirable, and that is how positional hash arguments with non-Symbol keys are converted to keywords.  That breaks backwards compatibility with Ruby 2.6, and there is no reason to change behavior and emit a warning in Ruby 2.7 when the Ruby 3 behavior will be same as Ruby 2.6 in this case.

Ruby 2.6 and my branch:

```ruby
def a(x=1, **h)
  [x, h]
end

a({:a=>1})
# => [{}, {:a=>1}]

a({"a"=>1})
# [{"a"=>1}, {}]
```

There is still backwards compatibility breakage in cases where the last positional hash has both Symbol keys and non-Symbol keys.  In Ruby 2.6, those hashes would be split, but I'm not sure if we want to keep backwards compatibility and warn about that case in Ruby 2.7. Doing so would probably increase complexity significantly. My branch changes the behavior so that the positional hash is always treated as a positional argument if it contains a non-Symbol key:

Ruby 2.6:

```ruby
a({"a"=>1, :a=>1})
# => [{"a"=>1}, {:a=>1}]
```

My branch:

```ruby
a({"a"=>1, :a=>1})
# => [{"a"=>1, :a=>1}, {}]
```

I have fixed all issues in `lib` that caused warnings, and no changes are required in `ext`.  Most changes were in the csv library, with minor changes in net, rdoc, rubygems, tempfile, and tmpdir.  Here is a stat for issues in `lib`:

```
 lib/csv.rb                             | 36 ++++++++++++++++++------------------
 lib/csv/core_ext/array.rb              |  2 +-
 lib/csv/core_ext/string.rb             |  2 +-
 lib/csv/row.rb                         |  2 +-
 lib/csv/table.rb                       |  4 ++--
 lib/net/ftp.rb                         |  2 +-
 lib/net/protocol.rb                    |  2 +-
 lib/rdoc/generator/darkfish.rb         | 12 ++++++------
 lib/rubygems.rb                        |  2 +-
 lib/rubygems/commands/setup_command.rb |  2 +-
 lib/rubygems/package.rb                |  2 +-
 lib/tempfile.rb                        |  4 ++--
 lib/tmpdir.rb                          |  4 ++--
```

Here's a comparison with the changes mame's branch requires in `lib` and `ext`:

```
 ext/etc/extconf.rb                                     |   2 +-
 ext/json/lib/json/common.rb                            |  28 +++++++++++++++-------------
 ext/json/lib/json/generic_object.rb                    |   4 ++--
 ext/openssl/lib/openssl/ssl.rb                         |   4 ++--
 ext/psych/lib/psych.rb                                 |   2 +-
 ext/psych/lib/psych/core_ext.rb                        |   4 ++--
 lib/bundler/dsl.rb                                     |   6 +++---
 lib/bundler/runtime.rb                                 |   2 +-
 lib/cgi/core.rb                                        |   7 ++++---
 lib/cgi/html.rb                                        |  11 ++++++-----
 lib/csv.rb                                             |  24 ++++++++++++------------
 lib/csv/core_ext/array.rb                              |   2 +-
 lib/csv/core_ext/string.rb                             |   2 +-
 lib/csv/row.rb                                         |   2 +-
 lib/csv/table.rb                                       |   4 ++--
 lib/erb.rb                                             |   3 ++-
 lib/mkmf.rb                                            |   4 ++--
 lib/net/ftp.rb                                         |  10 +++++-----
 lib/net/http.rb                                        |   5 ++---
 lib/net/http/generic_request.rb                        |   2 +-
 lib/net/imap.rb                                        |   2 +-
 lib/net/protocol.rb                                    |   2 +-
 lib/open-uri.rb                                        |  23 ++++++++++++-----------
 lib/open3.rb                                           |  96 +++++++++++++++++++++++++-----------------------------------------------------------------------
 lib/rdoc/generator/darkfish.rb                         |  18 +++++++++---------
 lib/resolv.rb                                          |  18 +++++++-----------
 lib/rexml/document.rb                                  |   6 ++----
 lib/rexml/xpath.rb                                     |   2 +-
 lib/rss/parser.rb                                      |   7 +------
 lib/rss/rss.rb                                         |   9 ++-------
 lib/rubygems.rb                                        |   6 +++---
 lib/rubygems/command.rb                                |   4 ++--
 lib/rubygems/commands/install_command.rb               |   4 ++--
 lib/rubygems/commands/pristine_command.rb              |   4 ++--
 lib/rubygems/commands/setup_command.rb                 |   2 +-
 lib/rubygems/dependency_installer.rb                   |   4 ++--
 lib/rubygems/installer.rb                              |   8 ++++----
 lib/rubygems/package.rb                                |   2 +-
 lib/rubygems/request_set.rb                            |  10 +++++-----
 lib/rubygems/request_set/gem_dependency_api.rb         |  11 +++++------
 lib/rubygems/resolver/git_specification.rb             |   4 ++--
 lib/rubygems/resolver/lock_specification.rb            |   2 +-
 lib/rubygems/resolver/specification.rb                 |   8 ++++----
 lib/rubygems/test_case.rb                              |  18 +++++++++---------
 lib/rubygems/test_utilities.rb                         |  14 +++++++-------
 lib/tempfile.rb                                        |   8 ++++----
 lib/uri/file.rb                                        |   4 ++--
 lib/uri/ftp.rb                                         |   2 +-
 lib/uri/generic.rb                                     |  22 +++++++++++-----------
 lib/uri/http.rb                                        |   6 +++---
 lib/uri/mailto.rb                                      |   2 +-
 lib/yaml/store.rb                                      |   4 ++--
```

I have fixed all issues in `test` that caused warnings.  These were also much less extensive than in mame's branch.

I have fixed a few broken tests that expected a specific format for warning messages for invalid keywords, as well as a test that assumed TypeError for a non-Symbol keyword (an ArgumentError is now used).

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

* 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.

---Files--------------------------------
vm_args.diff (4.19 KB)
vm_args_v2.diff (4.18 KB)


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

  parent reply	other threads:[~2019-04-08  2:29 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 ` [ruby-core:90240] " merch-redmine
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 ` merch-redmine [this message]
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-77517.20190408022948.ee2f23ed4f9aab82@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).