Project

General

Profile

Feature #16037

Updated by sawa (Tsuyoshi Sawada) over 4 years ago

Pattern matching in `in` argument seems to prohibit multiple occurrences of single/double-splatted variables. 

 ```ruby 
 case ["a", "b", 3, "c", "d"]; in *foo, Integer, bar; end # >> (Not SyntaxError) 
 case ["a", "b", 3, "c", "d"]; in *foo, Integer, *bar; end # >> SyntaxError: unexpected * 
 ``` 

 However, unlike conventional constant/variable assignment, it makes sense to have multiple occurrences of single/double-splatted variables in a single pattern matching provided that we have a definite rule regarding whether the splats are greedy or not. 

 I propose the following. 

 1. Relax the syntax for pattern matching in `in` argument to allow multiple occurrences of single/double-splatted variables, and set up a rule regarding whether the splats are greedy or not; preferably greedy. 
 2. Further, introduce new syntax for non-greedy splats `*?foo`, `**?foo`. Currently, they are syntactically invalid, so I don't think they would conflict with existing code. 

 ```ruby 
 case ["a", "b", 3, "c", "d", 6, "e", "f"]; in *foo, Integer, *bar; foo end # => ["a", "b", 3, "c", "d"] 
 case ["a", "b", 3, "c", "d", 6, "e", "f"]; in *?foo, Integer, *bar; foo end # => ["a", "b"] 

Back