There are the following differences between case ... when and case ... in. Is this an expected behavior?
% ruby -v
ruby 3.1.0dev (2021-05-28T16:34:27Z master e56ba6231f) [x86_64-darwin19]
% ruby -ce 'case expression when 42;end'
Syntax OK
% ruby -ce 'case expression in 42;end'
-e:1: warning: One-line pattern matching is experimental, and the behavior may change in future versions of Ruby!
-e:1: syntax error, unexpected `end', expecting `when'
case expression in 42;end
So, I have two concerns.
Since the pattern matching syntax is different from case ... when, can't user write semicolon one-line case ... in in the same semicolon one-line as case ... when?
Does case expression in 42; end display an experimental warning of one-line pattern matching. Right?
This is reproduced in Ruby 3.1.0-dev and Ruby 3.0.1.
NOTE 1: I understand that only syntax that doesn't use case and end is experimental one-line pattern matching syntax.
% ruby -ce 'expression in 42'
-e:1: warning: One-line pattern matching is experimental, and the behavior may change in future versions of Ruby!
Syntax OK
NOTE 2: The syntax is OK if a semicolon is used between expression and in. But case ... when is a valid syntax to omit.
% ruby -e ruby -ce 'case expression; in 42; end'
Syntax OK
NOTE 1: I understand that only syntax that doesn't use case and end is experimental one-line pattern matching syntax.
A little supplement. The following is also an experimental one-line pattern matching syntax since Ruby 3.0, but the => one-line pattern matching syntax is no problem.
% ruby -e "'' =>''"
-e:1: warning: One-line pattern matching is experimental, and the behavior may change in future versions of Ruby!
I thought it a trade-off.
To allow the statement, one-line pattern matching is not allowed there.
But it returns true/false only and may be useless for case.
This is interesting. It surfaces an incompatibility we have. I didn't realize Ruby didn't allow this, so YARP allows case expression in 42; end to parse as expected by the OP here. If parse.y can't support this, I will need to explicitly disallow this in YARP.
It's possible https://github.com/yui-knk/ruby/tree/bugs_17925.
I think this is not limitaion but the matter of choice. Currently expr is expected after case therefore expression in 42 is interpreted as expr.
expr appears other place of the grammar. For example, if expr ..., while expr ... and so on. This change introduces an exception that you can write one line pattern matching after if, unless, while, until and so on but you can't after case.
New grammar introduces incompatibility. It changes the behavior of case ... in and breaks case ... when
It introduces an exceptional rule to the grammar
As a reporter says, there is workaround to add ; after expression
expression in 42 always returns true or false therefore it might be useless for case. Howerver we need to consider such impacts and benefits before decision making.
Impact on case ... when can be found as Shift/Reduce conflict. In this state, shift derives case ... when, on the other hand reduce derives case ... in.