From: daniel@...42.com Date: 2020-04-16T14:07:26+00:00 Subject: [ruby-core:97922] [Ruby master Feature#16670] Reverse order of `expression` in `pattern` for 1-line pattern matching while it's still experimental Issue #16670 has been updated by Dan0042 (Daniel DeLorme). I think the `in` syntax and order feels natural when you're actually matching a pattern. It's not AI-like pattern recognition like "find a pattern _in_ this" but rather pattern correspondence like "is this _in_ the range expressed by this pattern". But when using it only as destructuring assigment it does feel that something is off. Maybe it's because `expr in var` has the opposite order of `for var in expr`. So rather than reversing the order, I'd like to propose `~|>` as a more natural-feeling alias for rightward destructuring assignment. Full proposal at #16794 ---------------------------------------- Feature #16670: Reverse order of `expression` in `pattern` for 1-line pattern matching while it's still experimental https://bugs.ruby-lang.org/issues/16670#change-85145 * Author: ttilberg (Tim Tilberg) * Status: Open * Priority: Normal ---------------------------------------- Currently the 1-line syntax for pattern matching is: ``` # Usage: in expression = { pattern: "Example" } expression in {pattern: something} # something => "Example" ``` Is it technically possible, and desirable to switch the order of this syntax to: ``` # Usage: in expression = { pattern: "Example" } {pattern: something} in expression # something => "Example" ``` ? Here are my reasons: - It is more intuitive in English -- we are "finding a pattern in something". Finding "something in a pattern" doesn't seem to make sense. - Assignment is happening, and this keeps assignment on the left side of the operator which feels more natural. - It matches existing behavior with the workings of the case statement: Understanding that a `case` block evaluates each `when` expression using `when_expression === case_expression` makes more consistency with `when_pattern in case_pattern` using the new operator. ``` case something when /pattern/ end # is equivalent to /pattern/ === something # This creates more parity with case something in {pattern: x} # would be equivalent to {pattern: x} in something ``` Please see the following discussion on Reddit: https://www.reddit.com/r/ruby/comments/favshb/27s_pattern_matching_official_docs_recently_merged/fj2c7ng/ -- https://bugs.ruby-lang.org/ Unsubscribe: