ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: jonathan@hefner.pro
To: ruby-core@ruby-lang.org
Subject: [ruby-core:95191] [Ruby master Feature#15865] `<expr> in <pattern>` expression
Date: Wed, 02 Oct 2019 21:36:06 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-81865.20191002213604.cd0c83b612e86866@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15865.20190521015812@ruby-lang.org

Issue #15865 has been updated by jonathanhefner (Jonathan Hefner).


mame (Yusuke Endoh) wrote:

> The difficult part is that `<pattern>` is not distinguishable from `<expr>` for a parser.  For example, `[1, 2, x, y]` is valid not only as `<pattern>` but also as `<expr>`.
> So, putting a bare `<pattern>` before `<expr>` is impossible in terms of parser implementation.  If we put a prefix before `<pattern>` (for example, `in <pattern> <~ <expr>`), it may be feasible.

Yes, I see!  I'm sorry I missed that point from your previous comment.  I do think a prefixed syntax could help it read more naturally.  If the prefixed syntax was unambiguous enough, maybe `=` could be used as `<match-op>`.  For example, could `@[1, 2, x, y] = ary` be parsed unambiguously?

> IMO it strongly resembles ... assignment rather than pattern matching

I agree, but isn't pattern matching the same as (destructuring) assignment in this context?


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

* Author: mame (Yusuke Endoh)
* Status: Closed
* 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-10-02 21:36 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 ` [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 ` jonathan [this message]

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-81865.20191002213604.cd0c83b612e86866@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).