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=-4.1 required=3.0 tests=BAYES_00, 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 65A881F462 for ; Wed, 22 May 2019 07:38:46 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 6E8821209FB; Wed, 22 May 2019 16:38:41 +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 EF92A1209FB for ; Wed, 22 May 2019 16:38:39 +0900 (JST) Received: by filter0059p3mdw1.sendgrid.net with SMTP id filter0059p3mdw1-17544-5CE4FC81-F 2019-05-22 07:38:41.145898767 +0000 UTC m=+550791.295749205 Received: from herokuapp.com (unknown [3.81.19.97]) by ismtpd0049p1mdw1.sendgrid.net (SG) with ESMTP id K7SHlyY4SZ2GY6_qp_vBfQ for ; Wed, 22 May 2019 07:38:41.090 +0000 (UTC) Date: Wed, 22 May 2019 07:38:41 +0000 (UTC) From: ko1@atdot.net Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 68255 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15865 X-Redmine-Issue-Author: mame X-Redmine-Issue-Assignee: matz X-Redmine-Sender: ko1 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?fVTMYOBjtdvXNcWwrscBhLsHItUXVK5L4mtnq0mdcRdO0M+9V=2Fkqyq=2F9Q3uEy7?= =?us-ascii?Q?yHQWN+j7qC3+be77Tbg5IpQWyeLJ95E4JCD3J6m?= =?us-ascii?Q?HVfQfXRyWiskJRcDelRsQAI7XaQ7AiDFQ2XVYto?= =?us-ascii?Q?xOKXSgii6WzCbsmBu6XtvbiCi9BYn5WBmemkoHT?= =?us-ascii?Q?tkXwEy7+4mik3?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 92769 Subject: [ruby-core:92769] [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 ko1 (Koichi Sasada). Trivial comment. We can't introduce guard pattern (`pat if expr`) because it will conflict with `expr if cond`. ---------------------------------------- Feature #15865: ` in ` expression https://bugs.ruby-lang.org/issues/15865#change-78134 * 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/