ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:111201] [Ruby master Feature#10343] Postfix notations for `when` and `else` inside `case` statement
       [not found] <redmine.issue-10343.20141008164245.2963@ruby-lang.org>
@ 2022-12-04 20:57 ` sawa (Tsuyoshi Sawada)
  2023-02-11  6:43 ` [ruby-core:112359] " rubyFeedback (mark potter) via ruby-core
  2023-03-10  1:16 ` [ruby-core:112815] " mame (Yusuke Endoh) via ruby-core
  2 siblings, 0 replies; 3+ messages in thread
From: sawa (Tsuyoshi Sawada) @ 2022-12-04 20:57 UTC (permalink / raw)
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [ruby-core:112359] [Ruby master Feature#10343] Postfix notations for `when` and `else` inside `case` statement
       [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
  2023-03-10  1:16 ` [ruby-core:112815] " mame (Yusuke Endoh) via ruby-core
  2 siblings, 0 replies; 3+ messages in thread
From: rubyFeedback (mark potter) via ruby-core @ 2023-02-11  6:43 UTC (permalink / raw)
  To: ruby-core; +Cc: rubyFeedback (mark potter)

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/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [ruby-core:112815] [Ruby master Feature#10343] Postfix notations for `when` and `else` inside `case` statement
       [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 ` [ruby-core:112359] " rubyFeedback (mark potter) via ruby-core
@ 2023-03-10  1:16 ` mame (Yusuke Endoh) via ruby-core
  2 siblings, 0 replies; 3+ messages in thread
From: mame (Yusuke Endoh) via ruby-core @ 2023-03-10  1:16 UTC (permalink / raw)
  To: ruby-core; +Cc: mame (Yusuke Endoh)

Issue #10343 has been updated by mame (Yusuke Endoh).

Status changed from Open to Rejected

Discussed at the dev meeting. @matz rejected this proposal because he didn't think it was easy to understand.

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

* Author: sawa (Tsuyoshi Sawada)
* Status: Rejected
* 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/

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-03-10  1:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [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 ` [ruby-core:112359] " rubyFeedback (mark potter) via ruby-core
2023-03-10  1:16 ` [ruby-core:112815] " 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 read-only IMAP folder(s) and NNTP newsgroup(s).