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, 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 971651F462 for ; Tue, 21 May 2019 16:22:42 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id B145A120929; Wed, 22 May 2019 01:22:37 +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 51EAE120972 for ; Wed, 22 May 2019 01:22:35 +0900 (JST) Received: by filter0179p3mdw1.sendgrid.net with SMTP id filter0179p3mdw1-24447-5CE425CC-15 2019-05-21 16:22:36.34016572 +0000 UTC m=+495839.317764944 Received: from herokuapp.com (unknown [3.90.36.146]) by ismtpd0010p1iad1.sendgrid.net (SG) with ESMTP id _T4U1HXNQ76fILKoKuiRFA for ; Tue, 21 May 2019 16:22:36.198 +0000 (UTC) Date: Tue, 21 May 2019 16:22:36 +0000 (UTC) From: mame@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 68231 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15865 X-Redmine-Issue-Author: mame X-Redmine-Issue-Assignee: matz X-Redmine-Sender: mame 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?EJh2gqwnyqXtd++xo=2FinyA1V0bXouTB4FkWnzNiKb49x73BSKYdkPn0D+38QLB?= =?us-ascii?Q?MqskO6Qy22PpIKE2nHSEaAtHDUrDAkWpJxizBsH?= =?us-ascii?Q?LuoVc420faLvAm2vIIPyXegZ8Z6Bx1bi0fPL4Dh?= =?us-ascii?Q?sOlOZu4pUkFkK+BBptJcu+e4MHTZ1NDp0dYFpGE?= =?us-ascii?Q?UHBn+hZSvtuzFv+ibpo7hfoZRi5jcsjF=2Feg=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 92746 Subject: [ruby-core:92746] [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 mame (Yusuke Endoh). Description updated Oops, the first example was wrong. Fixed. ``` -[1, 2, 3] in 1, 2, 3 #=> false +[1, 2, 3] in 1, 2, 4 #=> false ``` ---------------------------------------- Feature #15865: ` in ` expression https://bugs.ruby-lang.org/issues/15865#change-78108 * 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/