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.1 required=3.0 tests=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=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 A41671F462 for ; Tue, 4 Jun 2019 21:56:59 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 4D5281208CC; Wed, 5 Jun 2019 06:56:51 +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 49512120887 for ; Wed, 5 Jun 2019 06:56:48 +0900 (JST) Received: by filter0092p3las1.sendgrid.net with SMTP id filter0092p3las1-23273-5CF6E921-11 2019-06-04 21:56:49.472162845 +0000 UTC m=+16028.569600810 Received: from herokuapp.com (unknown [52.23.244.33]) by ismtpd0005p1iad2.sendgrid.net (SG) with ESMTP id QA4nb3pWTBqVsA4yPEWCvQ for ; Tue, 04 Jun 2019 21:56:49.265 +0000 (UTC) Date: Tue, 04 Jun 2019 21:56:49 +0000 (UTC) From: eregontp@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 68441 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15865 X-Redmine-Issue-Author: mame 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?KippOI8ZHtTweq7XfQzW93937kJ4QNWwSBuHnaMEcr0Sz0lFGw0VkeIZIayWnA?= =?us-ascii?Q?OnglDz9UfMBD0R80iuDdIl0mUJQHoiB0nJLKX1z?= =?us-ascii?Q?3IdMqDYOCKB257UInu+ybNoTaK3dDQpY+WibKQy?= =?us-ascii?Q?VLYuMFwepgX9jZOfef=2Fg=2FnzxH5p9PCTf2iJofyZ?= =?us-ascii?Q?7sVD6K4cFij1kX18jn26Om4vE1nikXRgKYQ=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 92964 Subject: [ruby-core:92964] [Ruby trunk Feature#15865] ` in ` expression 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 #15865 has been updated by Eregon (Benoit Daloze). mame (Yusuke Endoh) wrote: > * Incomplete pattern matching also rewrites variables: `[1, 2, 3] in x, 42, z` will write 1 to the variable "x". This behavior is the same as the current "case...in". This sounds concerning to me. With case/in, it's clear where the variables should be defined, and it's a matter of fixing it so the variables are only defined in that `in pattern` branch, and `nil` in other branches. (IMHO we should fix that for 2.7, otherwise it will be a compatibility issue). But here it's unclear how long variables in inline patterns should live. Probably for the whole method? Or just for the `if`? E.g.: ```ruby json = { name: "Me", age: 28, hobby: :ruby } if json in { name:, age: (...20) } ... end if json in { name:, hobby: } # BUG: age should not be set here puts "Hello #{name}, you enjoy #{hobby} and are #{age || "unknown"} years old" end ``` When used as `if expr in pattern`, I think it is natural to expect no significant side effects, but changing local variables for a partial match seems kind of problematic. ---------------------------------------- Feature #15865: ` in ` expression https://bugs.ruby-lang.org/issues/15865#change-78342 * Author: mame (Yusuke Endoh) * Status: Open * Priority: Normal * Assignee: matz (Yukihiro Matsumoto) * Target version: ---------------------------------------- How about adding a syntax for one-line pattern matching: ` in ` ? ``` [1, 2, 3] in x, y, z #=> true (with assigning 1 to x, 2 to y, and 3 to z) [1, 2, 3] in 1, 2, 4 #=> false ``` More realistic example: ``` json = { name: "ko1", age: 39, address: { postal: 123, city: "Taito-ku" } } if json in { name:, age: (20..), address: { city: "Taito-ku" } } p name #=> "ko1" else raise "wrong format" end ``` It is simpler and more composable than "case...in" when only one "in" clause is needed. I think that in Ruby a pattern matching would be often used for "format-checking", to check a structure of data, and this use case would usually require only one clause. This is the main rationale for the syntax I propose. Additional two small rationales: * It may be used as a kind of "right assignment": `1 + 1 in x` behaves like `x = 1 + 1`. It returns true instead of 2, though. * There are some arguments about the syntax "case...in". But if we have ` in `, "case...in" can be considered as a syntactic sugar that is useful for multiple-clause cases, and looks more natural to me. There are two points I should note: * ` in ` is an expression like ` and `, so we cannot write it as an argument: `foo(1 in 1)` causes SyntaxError. You need to write `foo((1 in 1))` as like `foo((1 and 1))`. I think it is impossible to implement. * Incomplete pattern matching also rewrites variables: `[1, 2, 3] in x, 42, z` will write 1 to the variable "x". This behavior is the same as the current "case...in". Nobu wrote a patch: https://github.com/nobu/ruby/pull/new/feature/expr-in-pattern -- https://bugs.ruby-lang.org/