* [ruby-core:120106] [Ruby master Bug#20931] Using `in` as an expression requires extra parentheses
@ 2024-12-04 21:36 stephenprater (Stephen Prater) via ruby-core
2024-12-04 22:46 ` [ruby-core:120107] " alanwu (Alan Wu) via ruby-core
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: stephenprater (Stephen Prater) via ruby-core @ 2024-12-04 21:36 UTC (permalink / raw)
To: ruby-core; +Cc: stephenprater (Stephen Prater)
Issue #20931 has been reported by stephenprater (Stephen Prater).
----------------------------------------
Bug #20931: Using `in` as an expression requires extra parentheses
https://bugs.ruby-lang.org/issues/20931
* Author: stephenprater (Stephen Prater)
* Status: Open
* ruby -v: 3.3.1
* Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
TBH - I'm not sure if this is a bug or not - but it certainly surprising behavior and I'd at least like to understand it.
Given a hash t - that can be pattern matched: `t = {a: 1, b:1 }`
``` ruby
r = t in {a: 1, c:1 } # returns `false`
r # {a: 1, c: 1} wat
```
Presumably this is because `=` binds higher than `in` - so that expression is equivalent to `(r = t) in {a: 1, c: 1}`
But in that case - why does using the results of `in` require an additional set of parentheses to avoid a syntax error when the result of the expression is used as an argument to a method?
``` ruby
puts(t in {a: 1, c: 1}) # syntax error
puts((t in {a: 1, c: 1}) # false
```
Especially since this works fine:
``` ruby
puts(case t; in { a: 1, c:1 }; true; else false; end)
```
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 4+ messages in thread
* [ruby-core:120107] [Ruby master Bug#20931] Using `in` as an expression requires extra parentheses
2024-12-04 21:36 [ruby-core:120106] [Ruby master Bug#20931] Using `in` as an expression requires extra parentheses stephenprater (Stephen Prater) via ruby-core
@ 2024-12-04 22:46 ` alanwu (Alan Wu) via ruby-core
2024-12-04 23:04 ` [ruby-core:120108] " stephenprater (Stephen Prater) via ruby-core
2024-12-04 23:15 ` [ruby-core:120109] " mame (Yusuke Endoh) via ruby-core
2 siblings, 0 replies; 4+ messages in thread
From: alanwu (Alan Wu) via ruby-core @ 2024-12-04 22:46 UTC (permalink / raw)
To: ruby-core; +Cc: alanwu (Alan Wu)
Issue #20931 has been updated by alanwu (Alan Wu).
Status changed from Open to Rejected
I'm closing this since I'm pretty sure this isn't a bug. An imperfect explanation follows. Feel free to jump in if anyone has a better explanation.
To understand the precedence, note that `in` has a symbolic friend `=>`, and much like how `or` binds lower than `||`, `in` binds lower than `=>`. (Runtime behavior of `=>` and `in` are different, though.)
As for why it requires parentheses in argument context, it's consistent with other single word English keywords such as `and`, `or`, `if`, and `unless`:
<details>
```shell
$ for keyword in and or if unless; do ruby -vc -e "puts(1 $keyword 1)"; done
ruby 3.4.0dev (2024-12-04T21:26:31Z master c0e12bf8d2) +PRISM [arm64-darwin24]
ruby: -e:1: syntax errors found (SyntaxError)
> 1 | puts(1 and 1)
| ^~~ unexpected 'and'; expected a `)` to close the arguments
| ^ unexpected ')', ignoring it
| ^ unexpected ')', expecting end-of-input
2 |
ruby 3.4.0dev (2024-12-04T21:26:31Z master c0e12bf8d2) +PRISM [arm64-darwin24]
ruby: -e:1: syntax errors found (SyntaxError)
> 1 | puts(1 or 1)
| ^~ unexpected 'or'; expected a `)` to close the arguments
| ^ unexpected ')', ignoring it
| ^ unexpected ')', expecting end-of-input
2 |
ruby 3.4.0dev (2024-12-04T21:26:31Z master c0e12bf8d2) +PRISM [arm64-darwin24]
-e:1: warning: literal in condition
ruby: -e:1: syntax errors found (SyntaxError)
> 1 | puts(1 if 1)
| ^~ unexpected 'if'; expected a `)` to close the arguments
| ^ unexpected ')', ignoring it
| ^ unexpected ')', expecting end-of-input
2 |
ruby 3.4.0dev (2024-12-04T21:26:31Z master c0e12bf8d2) +PRISM [arm64-darwin24]
-e:1: warning: literal in condition
ruby: -e:1: syntax errors found (SyntaxError)
> 1 | puts(1 unless 1)
| ^~~~~~ unexpected 'unless'; expected a `)` to close the arguments
| ^ unexpected ')', ignoring it
| ^ unexpected ')', expecting end-of-input
2 |
```
</details>
Allowing these limit examples to work as expected probably causes parsing ambiguity in some other cases, so they're rejected. But I'm no parser expert.
----------------------------------------
Bug #20931: Using `in` as an expression requires extra parentheses
https://bugs.ruby-lang.org/issues/20931#change-110855
* Author: stephenprater (Stephen Prater)
* Status: Rejected
* ruby -v: 3.3.1
* Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
TBH - I'm not sure if this is a bug or not - but it certainly surprising behavior and I'd at least like to understand it.
Given a hash t - that can be pattern matched: `t = {a: 1, b:1 }`
``` ruby
r = t in {a: 1, c:1 } # returns `false`
r # {a: 1, c: 1} wat
```
Presumably this is because `=` binds higher than `in` - so that expression is equivalent to `(r = t) in {a: 1, c: 1}`
But in that case - why does using the results of `in` require an additional set of parentheses to avoid a syntax error when the result of the expression is used as an argument to a method?
``` ruby
puts(t in {a: 1, c: 1}) # syntax error
puts((t in {a: 1, c: 1}) # false
```
Especially since this works fine:
``` ruby
puts(case t; in { a: 1, c:1 }; true; else false; end)
```
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 4+ messages in thread
* [ruby-core:120108] [Ruby master Bug#20931] Using `in` as an expression requires extra parentheses
2024-12-04 21:36 [ruby-core:120106] [Ruby master Bug#20931] Using `in` as an expression requires extra parentheses stephenprater (Stephen Prater) via ruby-core
2024-12-04 22:46 ` [ruby-core:120107] " alanwu (Alan Wu) via ruby-core
@ 2024-12-04 23:04 ` stephenprater (Stephen Prater) via ruby-core
2024-12-04 23:15 ` [ruby-core:120109] " mame (Yusuke Endoh) via ruby-core
2 siblings, 0 replies; 4+ messages in thread
From: stephenprater (Stephen Prater) via ruby-core @ 2024-12-04 23:04 UTC (permalink / raw)
To: ruby-core; +Cc: stephenprater (Stephen Prater)
Issue #20931 has been updated by stephenprater (Stephen Prater).
That works for me - thanks for the explanation.
----------------------------------------
Bug #20931: Using `in` as an expression requires extra parentheses
https://bugs.ruby-lang.org/issues/20931#change-110856
* Author: stephenprater (Stephen Prater)
* Status: Rejected
* ruby -v: 3.3.1
* Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
TBH - I'm not sure if this is a bug or not - but it certainly surprising behavior and I'd at least like to understand it.
Given a hash t - that can be pattern matched: `t = {a: 1, b:1 }`
``` ruby
r = t in {a: 1, c:1 } # returns `false`
r # {a: 1, c: 1} wat
```
Presumably this is because `=` binds higher than `in` - so that expression is equivalent to `(r = t) in {a: 1, c: 1}`
But in that case - why does using the results of `in` require an additional set of parentheses to avoid a syntax error when the result of the expression is used as an argument to a method?
``` ruby
puts(t in {a: 1, c: 1}) # syntax error
puts((t in {a: 1, c: 1}) # false
```
Especially since this works fine:
``` ruby
puts(case t; in { a: 1, c:1 }; true; else false; end)
```
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 4+ messages in thread
* [ruby-core:120109] [Ruby master Bug#20931] Using `in` as an expression requires extra parentheses
2024-12-04 21:36 [ruby-core:120106] [Ruby master Bug#20931] Using `in` as an expression requires extra parentheses stephenprater (Stephen Prater) via ruby-core
2024-12-04 22:46 ` [ruby-core:120107] " alanwu (Alan Wu) via ruby-core
2024-12-04 23:04 ` [ruby-core:120108] " stephenprater (Stephen Prater) via ruby-core
@ 2024-12-04 23:15 ` mame (Yusuke Endoh) via ruby-core
2 siblings, 0 replies; 4+ messages in thread
From: mame (Yusuke Endoh) via ruby-core @ 2024-12-04 23:15 UTC (permalink / raw)
To: ruby-core; +Cc: mame (Yusuke Endoh)
Issue #20931 has been updated by mame (Yusuke Endoh).
As for the limitation of `in`, there is a more easy-to-understand explanation. Consider `foo(a in 1, 2, 3)`. This is very ambiguous because there are three possible interpretation: `foo((a in 1), 2, 3)`, `foo((a in 1, 2), 3)`, and `foo((a in 1, 2, 3))`. Note that `a in 1, 2, 3` returns true when `a = [1, 2, 3]`. So parentheses are necessary.
I understand that it is confusing to need double parentheses when you use `in` as a simple expression without following commas. But no good solution came to mind.
----------------------------------------
Bug #20931: Using `in` as an expression requires extra parentheses
https://bugs.ruby-lang.org/issues/20931#change-110857
* Author: stephenprater (Stephen Prater)
* Status: Rejected
* ruby -v: 3.3.1
* Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
TBH - I'm not sure if this is a bug or not - but it certainly surprising behavior and I'd at least like to understand it.
Given a hash t - that can be pattern matched: `t = {a: 1, b:1 }`
``` ruby
r = t in {a: 1, c:1 } # returns `false`
r # {a: 1, c: 1} wat
```
Presumably this is because `=` binds higher than `in` - so that expression is equivalent to `(r = t) in {a: 1, c: 1}`
But in that case - why does using the results of `in` require an additional set of parentheses to avoid a syntax error when the result of the expression is used as an argument to a method?
``` ruby
puts(t in {a: 1, c: 1}) # syntax error
puts((t in {a: 1, c: 1}) # false
```
Especially since this works fine:
``` ruby
puts(case t; in { a: 1, c:1 }; true; else false; end)
```
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2024-12-04 23:15 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-12-04 21:36 [ruby-core:120106] [Ruby master Bug#20931] Using `in` as an expression requires extra parentheses stephenprater (Stephen Prater) via ruby-core
2024-12-04 22:46 ` [ruby-core:120107] " alanwu (Alan Wu) via ruby-core
2024-12-04 23:04 ` [ruby-core:120108] " stephenprater (Stephen Prater) via ruby-core
2024-12-04 23:15 ` [ruby-core:120109] " mame (Yusuke Endoh) via ruby-core
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 NNTP newsgroup(s).