ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: mame@ruby-lang.org
To: ruby-core@ruby-lang.org
Subject: [ruby-core:93381] [Ruby trunk Feature#15865] `<expr> in <pattern>` expression
Date: Thu, 27 Jun 2019 02:47:39 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-78904.20190627024738.f62d9a57db028add@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15865.20190521015812@ruby-lang.org

Issue #15865 has been updated by mame (Yusuke Endoh).


Thank you for your comment.

This ticket discusses one-line pattern matching: `<expr> in <pattern>` in my proposal.  This proposal is based on the `case/in` pattern-matching statement.   I'd like to avoid to discuss a common topic between one-line and `case/in` in this particular ticket.

You have three points.

1. Matching is not strict.
2. The proposed <match-op> is `in`.
3. The pattern is on the RHS.

The design choice of (1) is done in `case/in` statement.  The one-line version and `case/in` should share the semantics of each pattern.  It is unarguably too confusing if they have different semantics.  So, I like not to discuss this issue in this ticket.

So, you have two concerns for my proposal about one-line pattern matching syntax: (2) the keyword `in` and (3) the word order.  I understand that it is unnatural in English.  (Honestly, it is mathematically/logically correct as you said, so it works for me, a non-native, though.)  However, I believe that this is the best choice for some reasons:

* The appearance of `case/in` statement is `case <expr>; in <pattern>; ... end`.  In a sense, the keyword `in` and the order that `<pattern>` follows the keyword `in` are already established, as long as we accept `case/in` statement.

* The order `<pattern> <match-op> <expr>` is very difficult (or maybe impossible?) to implement parsing rules.  Perhaps we need to use `<pattern-marker> <pattern> <match-op> <expr>`, but it is very tough to agree with symbol or keyword of `<pattern-marker>` and `<match-op>`.

You can discuss the grand design of pattern matching and the syntax of `case/in` statement in #14912.  If the grand design is overturned, my proposal will need to change drastically.  (But in short, I respect the design decision.  I'm not fully satisfied with the syntax, but we spent very long time to discuss what keyword is suitable and feasible, and finally chose the design including the keyword `in`.)

----------------------------------------
Feature #15865: `<expr> in <pattern>` expression
https://bugs.ruby-lang.org/issues/15865#change-78904

* 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  2:47 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 ` [ruby-core:93378] " pvande
2019-06-27  2:47 ` mame [this message]
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-78904.20190627024738.f62d9a57db028add@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).