Feature #10343
closed
Postfix notations for `when` and `else` inside `case` statement
Added by sawa (Tsuyoshi Sawada) about 10 years ago.
Updated over 1 year ago.
Description
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.
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:
case foo
"a" when some_very_long_proc
"bbb" when short_regex
"cc" when some_long_regex
"dddd" else
end
Pros are:
-
Postfix notation does not require then
or ;
, so it is concise.
-
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.
-
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.
-
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.
I'm not sure how we can implement this.. but it looks not bad for me.
What I afraid of most is ..being able to parse this syntax well?
Aside from that, we can achieve what you want for now.
case foo
when some_very_long_condition then "a"
when short_cond then "bb"
when some_long_condition then "ccc"
else "dddd"
end
I have to admire this looks little bit ugly : (
Thank you.
- Description updated (diff)
- Description updated (diff)
After eight years, I still think this would be a good feature. Any opinions?
- Description updated (diff)
- Description updated (diff)
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.)
- Status changed from Open to Rejected
Discussed at the dev meeting. @matz (Yukihiro Matsumoto) rejected this proposal because he didn't think it was easy to understand.
Also available in: Atom
PDF
Like1
Like0Like0Like0Like1Like0Like0Like0Like0