archive (unofficial mirror)
 help / color / Atom feed
Subject: [ruby-core:95772] [Ruby master Feature#16296] Alternative behavior for `...` in method body if `...` is not in method definition
Date: Sun, 10 Nov 2019 10:20:39 +0000 (UTC)
Message-ID: <> (raw)
In-Reply-To: <>

Issue #16296 has been updated by Eregon (Benoit Daloze).

On current master `def m(a, ...); end` is a syntax error.
So I think we need to confirm first whether we will allow any other parameter alongside `...`.

Feature #16296: Alternative behavior for `...` in method body if `...` is not in method definition

* Author: Dan0042 (Daniel DeLorme)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
In #16253 we settled on a syntax where the remainder arguments captured via `...` in the method definition can be forwarded via `...` in the method body. I think that was the correct decision.

But I can't forget about the use case [presented by zverok]( (and in #15049) where the method definition is used to specify mandatory and default parameters and then forward all of them to another method. I've also experienced that same use case in my code. Using the current syntax we would need to do this:

def get(path:, accept: :json, headers: {}, ...)
  _request(method: :get, path: path, accept: accept, headers: headers, ...)

def post(path:, body:, accept: :json, headers: {}, ...)
  _request(method: :post, path: path, body: body, accept: accept, headers: headers, ...)

Which feels pointlessly repetitive to me. So I was thinking that maybe if `...` is not present in the method definition, then in the method body `...` could take on the meaning of "all arguments of the method". Then the code would look like this:

def get(path:, accept: :json, headers: {}, **opts)
  _request(method: :get, ...)

def post(path:, body:, accept: :json, headers: {}, **opts)
  _request(method: :post, ...)

In those examples (no positional parameters) it would also allow `Hash[...]` or `{}.replace(...)` to get the hash of all keyword arguments.

Pro: it allows a new useful and powerful behavior
Con: some may consider it 'unclean' to change the behavior of `...` based on the method definition


      parent reply index

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <>
2019-11-05 16:53 ` [ruby-core:95705] " daniel
2019-11-06  0:56 ` [ruby-core:95715] " shevegen
2019-11-10 10:20 ` eregontp [this message]

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:

  List information:

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

  git send-email \ \ \

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link archive (unofficial mirror)

Archives are clonable: git clone --mirror

Example config snippet for mirrors

Newsgroups are available over NNTP:

 note: .onion URLs require Tor:

AGPL code for this site: git clone