From: "mame (Yusuke Endoh) via ruby-core" <ruby-core@ml.ruby-lang.org>
To: ruby-core@ml.ruby-lang.org
Cc: "mame (Yusuke Endoh)" <noreply@ruby-lang.org>
Subject: [ruby-core:120109] [Ruby master Bug#20931] Using `in` as an expression requires extra parentheses
Date: Wed, 04 Dec 2024 23:15:17 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-110857.20241204231516.9233@ruby-lang.org> (raw)
In-Reply-To: <redmine.issue-20931.20241204213610.9233@ruby-lang.org>
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/
prev parent reply other threads:[~2024-12-04 23:15 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-04 21:36 [ruby-core:120106] " 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 [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-110857.20241204231516.9233@ruby-lang.org \
--to=ruby-core@ml.ruby-lang.org \
--cc=noreply@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).