Project

General

Profile

Actions

Feature #16355

closed

Raise NoMatchingPatternError when `expr in pat` doesn't match

Added by ktsj (Kazuki Tsujimoto) almost 3 years ago. Updated almost 3 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:95889]

Description

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) expr-in-pat-raises-error.patch ktsj (Kazuki Tsujimoto), 11/20/2019 12:58 AM

Related issues 3 (1 open2 closed)

Related to Ruby master - Feature #15865: `<expr> in <pattern>` expressionClosedmatz (Yukihiro Matsumoto)Actions
Related to Ruby master - Feature #16370: Pattern matching with variable assignment (the priority of `in` operator)OpenActions
Related to Ruby master - Feature #17371: Reintroduce `expr in pat`ClosedActions
Actions

Also available in: Atom PDF