ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: matz@ruby.or.jp
To: ruby-core@ruby-lang.org
Subject: [ruby-core:92093] [Ruby trunk Feature#14183] "Real" keyword argument
Date: Mon, 01 Apr 2019 14:52:35 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-77422.20190401145234.0c4cb90375ddf028@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-14183.20171214065906@ruby-lang.org

Issue #14183 has been updated by matz (Yukihiro Matsumoto).


@jeremyevans0 I **will** investigate your proposal. I was not fully satisfied with the complete separation model proposed for 3.0, but I didn't think of any other model which is intuitive and clean, especially considering static type analysis. Your proposal could be a better alternative.

Matz.


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

* 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-01 14:52 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 ` matz [this message]
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-77422.20190401145234.0c4cb90375ddf028@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).