[ruby-core:96006] [Ruby master Feature#16355] Raise NoMatchingPatternError when `expr in pat` doesn't match

[email protected]
Newsgroups gmane.comp.lang.ruby.core
Message-ID <redmine.journal-82842.20191128042542.df5b7d810004953e@ruby-lang.org>
Issue #16355 has been updated by matz (Yukihiro Matsumoto).


I accept this proposal for two reasons:
* as OP described, returning boolean values from `in` pattern matching masks match failure. This can cause serious problems.
* By this change, we cannot use `in` pattern matching in `if` conditionals. But this style can be easily expressed by `case` pattern matching.

Matz.

----------------------------------------
Feature #16355: Raise NoMatchingPatternError when `expr in pat` doesn't match
https://bugs.ruby-lang.org/issues/16355#change-82842

* Author: ktsj (Kazuki Tsujimoto)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
Currently, single line pattern matching(`expr in pat`) returns true or false.

```
[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
```

I think `expr in pat` should raise an exception when it doesn't match.
Because if a user doesn't check the return value of `expr in pat`, matching failure occurs implicitly and it may cause problems in subsequent processes.

```
expr in [0, x] # A user expects it always matches, but if it doesn't match...
...
(snip)
...
x.foo #=> NoMethodError (undefined method `foo' for nil:NilClass)
```

I also propose that `expr in pat` returns the result of `expr` if it matches.
It is similar to assignment.
	
```
x, y, z = 1, 2, 3      #=> [1, 2, 3]
[1, 2, 3] in [x, y, z] #=> [1, 2, 3]
```


---Files--------------------------------
expr-in-pat-raises-error.patch (2.59 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.