ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: pvande@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:93378] [Ruby trunk Feature#15865] `<expr> in <pattern>` expression
Date: Thu, 27 Jun 2019 00:21:09 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-78902.20190627002108.1f3787d5b09d6e29@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15865.20190521015812@ruby-lang.org

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: `<expr> in <pattern>` 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: `<expr> in <pattern>` ?

```
[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 `<expr> in <pattern>`, "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:

* `<expr> in <pattern>` is an expression like `<expr> and <expr>`, 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/

  parent reply	other threads:[~2019-06-27  0:21 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-15865.20190521015812@ruby-lang.org>
2019-05-21  1:58 ` [ruby-core:92733] [Ruby trunk Feature#15865] `<expr> in <pattern>` expression mame
2019-05-21  8:21 ` [ruby-core:92739] " shevegen
2019-05-21 16:22 ` [ruby-core:92746] " mame
2019-05-21 18:48 ` [ruby-core:92747] " eregontp
2019-05-22  7:38 ` [ruby-core:92769] " ko1
2019-05-22  8:04 ` [ruby-core:92773] " nobu
2019-05-22  8:54 ` [ruby-core:92775] " matz
2019-05-22 10:10 ` [ruby-core:92781] " eregontp
2019-06-04 21:56 ` [ruby-core:92964] " eregontp
2019-06-27  0:21 ` pvande [this message]
2019-06-27  2:47 ` [ruby-core:93381] " mame
2019-06-27  6:23 ` [ruby-core:93383] " pvande
2019-07-11  4:21 ` [ruby-core:93659] [Ruby master " manga.osyo
2019-07-30  4:23 ` [ruby-core:94022] " matz
2019-09-29 19:10 ` [ruby-core:95149] " jonathan
2019-10-01  6:25 ` [ruby-core:95167] " mame
2019-10-02 21:36 ` [ruby-core:95191] " jonathan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-78902.20190627002108.1f3787d5b09d6e29@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).