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.8 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 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 9EBD71F463 for ; Tue, 24 Sep 2019 22:15:50 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 66290120A31; Wed, 25 Sep 2019 07:14:33 +0900 (JST) Received: from o1678948x4.outbound-mail.sendgrid.net (o1678948x4.outbound-mail.sendgrid.net [167.89.48.4]) by neon.ruby-lang.org (Postfix) with ESMTPS id DD842120A31 for ; Wed, 25 Sep 2019 07:14:31 +0900 (JST) Received: by filter0179p3mdw1.sendgrid.net with SMTP id filter0179p3mdw1-9132-5D8A954A-31 2019-09-24 22:14:34.269391475 +0000 UTC m=+90833.625687949 Received: from herokuapp.com (unknown [54.157.43.252]) by ismtpd0046p1mdw1.sendgrid.net (SG) with ESMTP id qt4Ngh5mQoaLtBcFBwTmqw for ; Tue, 24 Sep 2019 22:14:34.135 +0000 (UTC) Date: Tue, 24 Sep 2019 22:14:34 +0000 (UTC) From: eregontp@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 70624 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 16178 X-Redmine-Issue-Author: Eregon X-Redmine-Issue-Assignee: matz X-Redmine-Sender: Eregon 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?KippOI8ZHtTweq7XfQzW93937kJ4QNWwSBuHnaMEcr0GcPlHQ0Jyu0S3JNHc3e?= =?us-ascii?Q?=2F5n6qEcWa3R41TPN+2AbqjGa7L7AvouCJ77vAa9?= =?us-ascii?Q?QajNVELoZW0MERILv0bNz2j6qPqAGE31th+pPnZ?= =?us-ascii?Q?CICxAaqCmvdST1h7ropV2NSdpT0hHaqcpTEsA9O?= =?us-ascii?Q?wYpiO3bna2mMJZ3N9i3NX4KRDLeKiUawzvQ=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 95069 Subject: [ruby-core:95069] [Ruby master Bug#16178] Numbered parameters: _1 should be the same as |x| and _0 should not exist 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 #16178 has been updated by Eregon (Benoit Daloze). Dan0042 (Daniel DeLorme) wrote: > `proc{ |x,| }.arity` == 1, so `_1` is consistent with that. Which sounds like another bug to me, because that block happily accepts more arguments, and should be identical to `|x,*|`, which `proc {|x,*|}.arity # => -2`. What is your point? My point about arity was that if you add an argument, arity changes, and behavior changes too. Everyone understands adding an extra argument to a block (or lambda) might change semantics (e.g., `[1,[2,3]].map(&-> { _1; _2 })` is ArgumentError), isn't it? That's already the case today with named parameters. How do you argue that `_2` "takes the second parameter" but `_1` "extracts the first element of the first parameter"? How is that consistent? > In order to get the tuples' first value you would need to do `array_of_arrays.map{_2;_1}` because otherwise `_1` would mean the entire tuple. You would need to do `array_of_arrays.map { |x,| x }`. The tiny extra verbosity is warranted for dropping elements. Try adding 1 to each element of a 2 dimensional array (a matrix). `_0` must be used currently, but really taking the element as it is (`|x|`) are the only correct semantics in general if you do not know the specific element types. Why would the correct semantics in general need a different syntax (`_0` and not `_1`)? BTW, about typing, how would you type `enum.map { _1 }` with enum an `Enumerable[T]`? Isn't it impossible, because the behavior is inconsistent at runtime? > This argument is really weird. Is it really so unsufferable to use `_0` instead of `_1`? Yes, it's inconsistent and I'm pretty sure people would use `_1` like `|x|`, without realizing it's just broken when it's passed an Array. Do we want frequent bugs based on this instead of just having to use `|x,|` when wanting to drop elements? ---------------------------------------- Bug #16178: Numbered parameters: _1 should be the same as |x| and _0 should not exist https://bugs.ruby-lang.org/issues/16178#change-81702 * Author: Eregon (Benoit Daloze) * Status: Open * Priority: Normal * Assignee: matz (Yukihiro Matsumoto) * Target version: * ruby -v: ruby 2.7.0dev (2019-09-24T12:57:54Z master 0e84eecc17) [x86_64-linux] * Backport: 2.5: UNKNOWN, 2.6: UNKNOWN ---------------------------------------- Currently on trunk: ```ruby array = ["string", 42, [1, 2]] array.map { |x| x * 2 } # => ["stringstring", 84, [1, 2, 1, 2]] array.map { _1 * 2 } # => ["stringstring", 84, 2] ``` Oops, this trivial code just lost data and completely ignored the element class! This is clearly contrary to intuition and is very dangerous. Using `_0` instead has the correct behavior but it's clear we use 1-based indexing for numbered parameters, and it doesn't solve that `_1` has dangerous behavior. Basically the current behavior is that `_0` is the same as `|x|` and `_1` is the same as `|x,|`. `|x,|` is almost never used in Ruby, and for good reasons, it just throws away data/information/the class of the object. Such a dangerous operation should only be done when it's explicit, and the trailing comma in `|x,|` shows that, but `_1` does not. So let's make `_1` be `|x|` and remove `_0`. I am going to be harsh, but this discussion has gone too long without any serious written argument for the current behavior: I believe it's irrational and irresponsible to have `_1` be `|x,|`, it's just going to lead to nasty bugs. Try to convince me otherwise. If not, in one week I want to apply this change. >From the discussion in https://bugs.ruby-lang.org/issues/15723#note-127 and in https://bugs.ruby-lang.org/issues/15708 Some reactions to this behavior in https://twitter.com/eregontp/status/1115318993299083265 -- https://bugs.ruby-lang.org/