From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-2.6 required=3.0 tests=AWL,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY shortcircuit=no autolearn=no autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id BA5FF1F4B4 for ; Sat, 10 Apr 2021 07:47:44 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 6E695120BEF; Sat, 10 Apr 2021 16:46:38 +0900 (JST) Received: from xtrwkhkc.outbound-mail.sendgrid.net (xtrwkhkc.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id 25183120B5A for ; Sat, 10 Apr 2021 16:46:36 +0900 (JST) Received: by filterdrecv-59db977c98-qlpng with SMTP id filterdrecv-59db977c98-qlpng-16-60715811-15 2021-04-10 07:47:29.593739048 +0000 UTC m=+1349009.139349790 Received: from herokuapp.com (unknown) by geopod-ismtpd-2-1 (SG) with ESMTP id eMlyD-eqQxWqgeXy_QP2fQ for ; Sat, 10 Apr 2021 07:47:29.407 +0000 (UTC) Date: Sat, 10 Apr 2021 07:47:29 +0000 (UTC) From: jean.boussier@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 79415 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 17785 X-Redmine-Issue-Author: marcandre X-Redmine-Issue-Assignee: matz X-Redmine-Sender: byroot X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: =?us-ascii?Q?AchqQMoUBMcQgz7gop0XiYUiatGIY7E61JGsTL4FvjdWt0qpgTl6B6IF8aRP8U?= =?us-ascii?Q?cnJi6DuPssprNmi33y1R6elPnxhq5AeSU1f4Jt1?= =?us-ascii?Q?JPxyvo8jkG43YYuF7JgJmtuh1UtZVLm5Y8l9+R6?= =?us-ascii?Q?70xGLLKxhSZ+9d0UM50uGzP3VzZ7xpUb0kxdyAx?= =?us-ascii?Q?sKcgmgU8jsKwFnLr9xty1CdDWX8GSIysEOBjFIw?= =?us-ascii?Q?aSqiF5gDVS8RZ=2FtjE=3D?= To: ruby-core@ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== X-ML-Name: ruby-core X-Mail-Count: 103368 Subject: [ruby-core:103368] [Ruby master Feature#17785] Allow named parameters to be keywords X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #17785 has been updated by byroot (Jean Boussier). > the number of keywords is very low, which means that the cases where using a keyword as an argument name makes sense is also very low. Variable and keyword names are not purely random though, so I don't think this statistical reasoning makes that much sense. Especially since keywords reserved names that are short and popular: `end`, `class`, etc. If you define method that generate some HTML, a `class:` named parameters is common, if you define a method that deal with period of times, `end:` is common, `if:` is common for methods taking callbacks, etc. I'm not for adding extra syntax, but I agree with @marcandree that `binding.local_variable_get(:class)` is too slow to be used in many cases. It would be great if the parser or VM would just optimize it away, but I understand that it's currently very tricky because both `#binding` and `Binding#local_variable_get` could have been redefined. ---------------------------------------- Feature #17785: Allow named parameters to be keywords https://bugs.ruby-lang.org/issues/17785#change-91461 * 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/