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.6 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=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 18F311F461 for ; Thu, 27 Jun 2019 00:21:15 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id C1D871209AD; Thu, 27 Jun 2019 09:21:09 +0900 (JST) Received: from o1678916x28.outbound-mail.sendgrid.net (o1678916x28.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id C5DE7120945 for ; Thu, 27 Jun 2019 09:21:07 +0900 (JST) Received: by filter0093p3iad2.sendgrid.net with SMTP id filter0093p3iad2-3905-5D140BF5-6 2019-06-27 00:21:09.130119728 +0000 UTC m=+26773.867421897 Received: from herokuapp.com (unknown [34.238.242.194]) by ismtpd0044p1mdw1.sendgrid.net (SG) with ESMTP id nCbB22i5RGi4vv8i1MLE1Q for ; Thu, 27 Jun 2019 00:21:08.980 +0000 (UTC) Date: Thu, 27 Jun 2019 00:21:09 +0000 (UTC) From: pvande@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 68869 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15865 X-Redmine-Issue-Author: mame X-Redmine-Issue-Assignee: matz X-Redmine-Sender: pvande 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?DUspvD5U+KmfxQYFs5vIwFmv+32U2mUxaIBwbWdvEM2KRQGZnlKvEWud24hgwR?= =?us-ascii?Q?iT=2FFwfMpG8H4U+tlRyeWkTvooTbZ3kqYQncC5qB?= =?us-ascii?Q?WrhT5u7mfdi5gjFkMfzxxegeOMAwJnllwCoCgn1?= =?us-ascii?Q?6SzqTFF=2FbbwaJ14AA+SdlugOa9HeJkjd6SBapKy?= =?us-ascii?Q?1M2PHdrL22wFG?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 93378 Subject: [ruby-core:93378] [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 pvande (Pieter van de Bruggen). As suggested by Yusuke on Twitter, I'm posting a link to my own personal "wishlist" around pattern matching. I'm happy to discuss any points that might benefit from clarification. https://gist.github.com/pvande/822a1aba02e5347c39e8e0ac859d752b ---------------------------------------- Feature #15865: ` in ` expression https://bugs.ruby-lang.org/issues/15865#change-78902 * 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/