ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: mame@ruby-lang.org
To: ruby-core@ruby-lang.org
Subject: [ruby-core:88843] [Ruby trunk Feature#14183] "Real" keyword argument
Date: Wed, 05 Sep 2018 03:01:40 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-73885.20180905030139.8ab088de9894718a@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-14183.20171214065906@ruby-lang.org

Issue #14183 has been updated by mame (Yusuke Endoh).


Eregon (Benoit Daloze) wrote:
> I would think the number of methods like debug() is a tiny fraction of the number of places we'd need to change if hash-without-braces is no longer supported.
>
> IMHO such a method with rest + kwargs seems a bad design in the first place as the arguments are too complex. That debug method could only accept one argument for instance.

I think you are too familiar with the current weird keyword arguments.  The original and primary purpose of keyword arguments is an extension of existing methods.  It looks rather "too complex" for a mere addition of keyword parameters to disturb other parameters and to break existing calls.

That being said, I agree that the "cancer" of this issue is a combination of rest/optional agruments and keyword ones.  Another, more modest idea that I have is, to prohibit (or just warn) a method definition that has both rest/optional + keyword parameters.  I don't like this because this spoils the purpose of keyword arguments, though.


> Also, how should `foo(1, "foo" => "bar")` behave?
> Should it be like `foo(1, {"foo" => "bar"})`?

I think so.

> In this case the syntax is inconsistent with `foo(1, foo: "bar")` where having or leaving out the braces matter.

Braced hash and bare one are inconsistent, even in the current spec.

```
def foo(v=:default)
  p v
end

h={}
foo( **h )    #=> {}
foo({**h})    #=> {}
foo(1,  **h ) #=> 1
foo(1, {**h}) #=> wrong number of arguments (given 2, expected 0..1)
```

Note that `**{}` does not simply mean "no argument".  If it was "no argument", the above `foo(**h)` would print `:default` instead of `{}`.

> Or does the `=>` imply the braces?
> I believe all Rubyists are used to `foo(1, :foo => "bar")` and  `foo(1, foo: "bar")` being identical.

They will be still identical because it is determined not only syntactically but also dynamically: a key-value pair whose key is a Symbol, is handled as keyword argument.  This behavior is not new.  Ruby 2.5 even does it:

```
def foo(h1=nil, **h2)
  p [h1, h2]
end
foo("foo" => 1, :bar => 2, baz: 3) #=> [{"foo"=>1}, {:bar=>2, :baz=>3}]
```

(This behavior has been changed in trunk, but I'm unsure if it is determined or not.)


> BTW, `p foo: 1` will no longer work then, and `p({foo: 1})` would be required, which feels very *unlike* Ruby, and is just impractical when debugging.

I completely agree with this.  I showed the method `debug` as an example, but I don't think that `Kernel#p` itself should change.  Some existing APIs that people expect to accept both `foo(k:1)` and `foo({k:1})`, e.g, `ERB#result_with_hash`, Sequel's `where`, should be kept as well.

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

* 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-09-05  3:01 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 ` mame [this message]
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 ` [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-73885.20180905030139.8ab088de9894718a@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).