ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: marcandre-ruby-core@marc-andre.ca
To: ruby-core@ruby-lang.org
Subject: [ruby-core:103305] [Ruby master Feature#17785] Allow named parameters to be keywords
Date: Thu, 08 Apr 2021 15:08:40 +0000 (UTC)	[thread overview]
Message-ID: <redmine.issue-17785.20210408150840.182@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-17785.20210408150840.182@ruby-lang.org

Issue #17785 has been reported by marcandre (Marc-Andre Lafortune).

----------------------------------------
Feature #17785: Allow named parameters to be keywords
https://bugs.ruby-lang.org/issues/17785

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
We should allow named parameters to be keywords and use add a trailing `_` to the corresponding variable:

```ruby
def check(arg, class:)
  arg.is_a?(class_)
end

check(42, class: Integer) # => true
```

Currently, if we want such an API we have to use `**rest`:

```ruby
def check(arg, **rest)
  class_ = rest.fetch(:class) { raise ArgumentError('missing keyword: :class')}
  if rest.size > 1
    unknown = rest.keys - [:class]
    raise ArgumentError("unknown keyword(s): :#{unknown.join(', :')})
  end

  arg.is_a?(class_)
end
```

This is very verbose, much less convenient, much less readable, prevents `steep` from generating the proper signature, etc.

We should do the same for pattern match.



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

       reply	other threads:[~2021-04-08 15:08 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-08 15:08 marcandre-ruby-core [this message]
2021-04-08 15:21 ` [ruby-core:103307] [Ruby master Feature#17785] Allow named parameters to be keywords mame
2021-04-08 15:23 ` [ruby-core:103308] " zverok.offline
2021-04-08 16:55 ` [ruby-core:103312] " marcandre-ruby-core
2021-04-09 13:04 ` [ruby-core:103346] " jean.boussier
2021-04-09 17:59 ` [ruby-core:103350] " eregontp
2021-04-09 20:57   ` [ruby-core:103354] " Austin Ziegler
2021-04-10  0:06 ` [ruby-core:103360] " duerst
2021-04-10  3:20 ` [ruby-core:103362] " nobu
2021-04-10  7:47 ` [ruby-core:103368] " jean.boussier
2021-04-10 17:04 ` [ruby-core:103377] " marcandre-ruby-core
2021-04-19 19:17 ` [ruby-core:103516] " matheusrichardt
2021-06-09  0:08 ` [ruby-core:104209] " harrison.bachrach
2021-12-02 20:07 ` [ruby-core:106414] " ioquatix (Samuel Williams)
2021-12-13  3:25 ` [ruby-core:106630] " Dan0042 (Daniel DeLorme)
2021-12-13 21:31 ` [ruby-core:106645] " Eregon (Benoit Daloze)
2021-12-14 15:32 ` [ruby-core:106657] " matheusrich (Matheus Richard)
2021-12-14 15:47 ` [ruby-core:106660] " Dan0042 (Daniel DeLorme)
2021-12-14 16:44 ` [ruby-core:106668] " Eregon (Benoit Daloze)
2021-12-17 13:54 ` [ruby-core:106719] " Dan0042 (Daniel DeLorme)
2021-12-17 18:10 ` [ruby-core:106725] " Eregon (Benoit Daloze)

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.issue-17785.20210408150840.182@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).