Feature #10343
closedPostfix notations for `when` and `else` inside `case` statement
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
thenor;, 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
casestatement 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.