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=-3.9 required=3.0 tests=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY shortcircuit=no autolearn=ham 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 7CDAA1F4B4 for ; Sat, 10 Apr 2021 00:06:42 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 7FDE6120B34; Sat, 10 Apr 2021 09:05:37 +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 420D9120A8A for ; Sat, 10 Apr 2021 09:05:36 +0900 (JST) Received: by filterdrecv-59db977c98-l8n8b with SMTP id filterdrecv-59db977c98-l8n8b-14-6070EC05-70 2021-04-10 00:06:29.867166377 +0000 UTC m=+1321345.063391716 Received: from herokuapp.com (unknown) by ismtpd0200p1mdw1.sendgrid.net (SG) with ESMTP id wAInyJQPTOqYV6X-uJVuIg for ; Sat, 10 Apr 2021 00:06:29.747 +0000 (UTC) Date: Sat, 10 Apr 2021 00:06:29 +0000 (UTC) From: duerst@it.aoyama.ac.jp Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 79407 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: duerst 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?uQY=2F2xNrNfHHTWbKn6MBvvzfU5Pqk9I4lnOVb0CFDutjMsQ5vUR5E5dleZYXOH?= =?us-ascii?Q?phiLqjpZIMifHpSVTSXFptxBY9xhoDUdZ3jqE34?= =?us-ascii?Q?LLrbv0OFhRmu+CbO8w9oO0Ut2th3+e8GhrrSnXM?= =?us-ascii?Q?aRIf+Avr2g3SFEyjKaoBvssSoYXz87U2TuDc4dR?= =?us-ascii?Q?Wsq2EaUh9tfrT1850hgpYRiwlDynm6Y0DQvHAw8?= =?us-ascii?Q?Z1PuZYiqOUM7Va0Fo=3D?= To: ruby-core@ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== X-ML-Name: ruby-core X-Mail-Count: 103360 Subject: [ruby-core:103360] [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="iso-8859-1" Content-Transfer-Encoding: quoted-printable Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #17785 has been updated by duerst (Martin D=FCrst). I think it's not a good idea to introduce special syntax such as `class_` j= ust for the case where arguments are named with keywords. First, 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. Second, there are keyword= s such as `if` and `else` that are of very doubtful use as variable names a= nyway. Third, using keywords as variable names inherently increases the cog= nitive load on the reader and is a source for confusion. Also, the special meaning of the trailing underscore will be difficult to r= ecognize and understand for most people because it will appear so rarely. A= nd the `_` doesn't match well with the `:` in the argument list. And `_` al= so is already allowed, so at least in theory, there's a chance of compatibi= lity problems. And then there's good old `klass`, which did the job for decades. And for t= hose who don't like `klass`, there's `class_`. What's the problem of using = `class_` in the argument list if your plan is to use it in the body of the = method anyway? marcandre (Marc-Andre Lafortune) wrote in #note-3: > Clearly, `class_` is much simpler and much faster than `binding.local_var= iable_get(:class)`... What about finding something in between the two? E.g. even just introducing= `variable_get` as an alias to `binding.local_variable_get` would make this= easier to use. And if this really needs optimization, it could be done, to= o, but using a different argument name would solve the problem. With respect to `\`, it reminds me of older languages (such as m4, C, and T= eX) where there's a purely string-based level below (or before) the usual s= tructured syntax. Do we want Ruby to descend to that level? Escaping exists= inside strings because you don't want the range of data you can handle wit= h a programming language to be restricted by the syntax of the language its= elf. Also, escaping inside strings is frequent enough for everybody, and oc= curs in a very similar form across a wide range of programming languages, s= o that every programmer knows it. Backslashes in front of keywords would be= a whole different matter. There are programming languages where there are no reserved keywords. The o= ne I know and have used is PL/1. If not having any keywords would have been= a design goal of Ruby, I'm sure Matz would have found a way to get there. = But it wasn't, and I guess it isn't. And as far as I understand, this propo= sal doesn't get us there. In conclusion, I think this issue chases a phantom. The trade-off (rarely u= sed obscure syntax to solve a rarely occurring pseudo-problem) is not good.= It would introduce some very rarely used edge-case syntax, and wouldn't re= ally make the language any better. If there are no more urgent kinds of improvements to Ruby syntax that this = one, then we know Ruby is in a pretty good place! ---------------------------------------- Feature #17785: Allow named parameters to be keywords https://bugs.ruby-lang.org/issues/17785#change-91451 * 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) # =3D> true ``` Currently, if we want such an API we have to use `**rest`: ```ruby def check(arg, **rest) class_ =3D rest.fetch(:class) { raise ArgumentError('missing keyword: :cl= ass')} if rest.size > 1 unknown =3D 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 `s= teep` from generating the proper signature, etc. We should do the same for pattern match. -- = https://bugs.ruby-lang.org/ Unsubscribe: