From: dementiev.vm@... Date: 2019-11-20T20:07:40+00:00 Subject: [ruby-core:95899] [Ruby master Feature#16355] Raise NoMatchingPatternError when `expr in pat` doesn't match Issue #16355 has been updated by palkan (Vladimir Dementyev). Agree, that it could save users from unexpected behavior. On the other hand, raising an exception drastically limits the application of online pattern matching: it won't be possible to use it with `if ... else ...` or in `select/filter` statements (here is a great [example](https://bugs.ruby-lang.org/issues/15865#note-13)). If users won't to ensure that a pattern matches, they can write `(expr in ptrn) || raise "smtp"`. Having one-line patterns return true or false brings a lot of possibilities, IMO. P.S. I was actually scanning the tracker for the mentions of the following situation I've just encountered: ```ruby assert_block do {a:0, b: 1} in {a:, **nil} a.nil? #=> this is not nil, which confused me at first; but now I think that this is a less evil than raising an exception end ``` ---------------------------------------- Feature #16355: Raise NoMatchingPatternError when `expr in pat` doesn't match https://bugs.ruby-lang.org/issues/16355#change-82741 * 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: