From: "sawa (Tsuyoshi Sawada)" <noreply@ruby-lang.org>
To: ruby-core@ml.ruby-lang.org
Subject: [ruby-core:111201] [Ruby master Feature#10343] Postfix notations for `when` and `else` inside `case` statement
Date: Sun, 04 Dec 2022 20:57:30 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-100492.20221204205729.2963@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-10343.20141008164245.2963@ruby-lang.org
Issue #10343 has been updated by sawa (Tsuyoshi Sawada).
After eight years, I still think this would be a good feature. Any opinions?
----------------------------------------
Feature #10343: Postfix notations for `when` and `else` inside `case` statement
https://bugs.ruby-lang.org/issues/10343#change-100492
* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
In a `case` statement, the condition part in the branches do not have the same length in general, and especially, `else` is much shorter than the conditions (`when ...`). So when we write the condition and the return value in a single line, they are not aligned, and are hard to read.
```ruby
case foo
when some_very_long_condition then "a"
when short_cond then "bb"
when some_long_condition then "ccc"
else "dddd"
end
```
I propose to allow postfix notations with `when` and `else` (or `otherwise`) inside `case` statement as below:
```ruby
case foo
"a" when some_very_long_proc
"bb" when short_regex
"ccc" when some_long_regex
"dddd" else
end
```
Pros are:
1) Postfix notation does not require `then` or `;`, so it is concise.
2) The return values from the branches (e.g., `"a"`, `"bb"`, `"ccc"`, `"dddd"`) tend to be shorter and more uniformly lengthened than the conditions, hence they are somewhat close to being aligned naturally, making this easier to read.
3) We are usually more interested in the return value than the condition of a branch, especially when we are reading someone's code and are trying to grasp what the `case` statement does or returns.
4) This notation is closer to case-like conditional notations regularly used in mathematics:
```
┌ 1 (x = 0)
x! = │
└ x (x - 1)! (otherwise)
```
So it would be easier to read for those who are familiar with mathematics.
--
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/postorius/lists/ruby-core.ml.ruby-lang.org/
next parent reply other threads:[~2022-12-04 20:57 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <redmine.issue-10343.20141008164245.2963@ruby-lang.org>
2022-12-04 20:57 ` sawa (Tsuyoshi Sawada) [this message]
2023-02-11 6:43 ` [ruby-core:112359] [Ruby master Feature#10343] Postfix notations for `when` and `else` inside `case` statement rubyFeedback (mark potter) via ruby-core
2023-03-10 1:16 ` [ruby-core:112815] " mame (Yusuke Endoh) via ruby-core
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-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-100492.20221204205729.2963@ruby-lang.org \
--to=ruby-core@ruby-lang.org \
--cc=ruby-core@ml.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 read-only IMAP folder(s) and NNTP newsgroup(s).