Project

General

Profile

Actions

Feature #16037

open

Allow multiple single/double-splatted variables in `in` pattern matching and introduce non-greedy-splatted variables

Added by sawa (Tsuyoshi Sawada) over 5 years ago. Updated over 5 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:94108]

Description

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

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.
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"]
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0