ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "rubyFeedback (mark potter) via ruby-core" <ruby-core@ml.ruby-lang.org>
To: ruby-core@ml.ruby-lang.org
Cc: "rubyFeedback (mark potter)" <noreply@ruby-lang.org>
Subject: [ruby-core:112359] [Ruby master Feature#10343] Postfix notations for `when` and `else` inside `case` statement
Date: Sat, 11 Feb 2023 06:43:47 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-101802.20230211064347.2963@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-10343.20141008164245.2963@ruby-lang.org

Issue #10343 has been updated by rubyFeedback (mark potter).


I do not necessarily have any opinion in favour, but neither
in disfavour. Kazuki's old comment probably refers primarily
to syntax-wise how the "when" do not align, so on that note 
aligned "when" would look prettier. Actually the trailing 
"else" is the most confusing part to me; the "when" does not
seem that awkward, even though it is a bit.

It may be interesting to ask matz's opinion nonetheless either
way - if he were to start ruby from scratch, would he opt for
such a syntax; after all we have had other additions such as
endless method definitions or pattern matching. :)

(I for one would love to see case/when become a more "first-class
citizen" feature, where we could query its internal state at all
times, or use different case/when to be combined, a bit like 
treating them as objects with specific state and behaviour - but
I guess I should add a new request rather than repurpose
Tsuyoshi's issue here.)





----------------------------------------
Feature #10343: Postfix notations for `when` and `else` inside `case` statement
https://bugs.ruby-lang.org/issues/10343#change-101802

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
In a `case` statement, the condition part (`when ...`) in the branches do not have the same length in general, and especially, `else` is much shorter than the conditions. 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_proc then "a"
  when short_regex then "bbb"
  when some_long_regex then "cc"
  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
  "bbb" when short_regex
  "cc" 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/

  parent reply	other threads:[~2023-02-11  6:44 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 ` [ruby-core:111201] [Ruby master Feature#10343] Postfix notations for `when` and `else` inside `case` statement sawa (Tsuyoshi Sawada)
2023-02-11  6:43 ` rubyFeedback (mark potter) via ruby-core [this message]
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-101802.20230211064347.2963@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    --cc=noreply@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).