[ruby-core:104999] [Ruby master Feature#16182] Should `expr in a, b, c` be allowed or not?
From:
"matz (Yukihiro Matsumoto)" <noreply@...>
Date:
2021-08-19 07:32:25 UTC
List:
ruby-core #104999
Issue #16182 has been updated by matz (Yukihiro Matsumoto).
I agree with allowing to omit parentheses in the pattern.
Matz.
----------------------------------------
Feature #16182: Should `expr in a, b, c` be allowed or not?
https://bugs.ruby-lang.org/issues/16182#change-93398
* Author: mame (Yusuke Endoh)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
In #15865, a new syntax `<expr> in <pattern>` was introduced. By using this, we can write:
```
json = { foo: 1, bar: 2}
if json in { foo:, bar: }
p [foo, bar] #=> [1, 2]
end
```
However, we cannot write:
```
p(json in { foo:, bar: }) #=> expected: true, actual: syntax error
```
This is because `<expr> in <pattern>` is an expression but not an argument. For example, `foo(json in a, b, c)` is ambiguous: it is considered `foo((json in a), b, c)` and `foo((json in a, b, c))`.
What should we do?
1. Do nothing; we admit that it is a spec
2. Revert the feature
3. Disallow a pattern like `a, b, c` or `a:, b:, c:` in this one-line pattern matching syntax; we ask a user to write `json in [a, b, c]` or `json in {a:, b:, c:}`
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>