From: jonathan@... Date: 2019-09-29T19:10:42+00:00 Subject: [ruby-core:95149] [Ruby master Feature#15865] ` in ` expression Issue #15865 has been updated by jonathanhefner (Jonathan Hefner). > We want to ask English speaker about this proposal. Do you feel natural on this syntax? (I am a native English speaker.) The syntax feels confusing to me. When I read `X in Y`, I expect `Y` to be a collection of things, and `X` to be an element of that collection. For example, `2 in [1, 2, 3]` would return true. I understand that a pattern describes an abstract set, and a set is a collection, so I think I see the reason for choosing `in`. But it still feels awkward. I also agree it is too dangerous to introduce a new keyword, unfortunately. Here are some alternatives, off the top of my head: * ` >> ` ("shoveling" the values into the captured variables) * ` ~> ` * ` <~ ` * ` ~= ` (probably too confusing with `=~`) * ` :~ ` * ` := ` (has a strong connotation of "assignment" in other languages) * ` :=== ` (as a reference to `===`) * ` ?= ` (kind of like `+=`, `*=`, etc) * ` =? ` (can be negated if desired, i.e. `!?`) ---------------------------------------- Feature #15865: ` in ` expression https://bugs.ruby-lang.org/issues/15865#change-81790 * Author: mame (Yusuke Endoh) * Status: Closed * Priority: Normal * Assignee: matz (Yukihiro Matsumoto) * Target version: ---------------------------------------- How about adding a syntax for one-line pattern matching: ` in ` ? ``` [1, 2, 3] in x, y, z #=> true (with assigning 1 to x, 2 to y, and 3 to z) [1, 2, 3] in 1, 2, 4 #=> false ``` More realistic example: ``` json = { name: "ko1", age: 39, address: { postal: 123, city: "Taito-ku" } } if json in { name:, age: (20..), address: { city: "Taito-ku" } } p name #=> "ko1" else raise "wrong format" end ``` It is simpler and more composable than "case...in" when only one "in" clause is needed. I think that in Ruby a pattern matching would be often used for "format-checking", to check a structure of data, and this use case would usually require only one clause. This is the main rationale for the syntax I propose. Additional two small rationales: * It may be used as a kind of "right assignment": `1 + 1 in x` behaves like `x = 1 + 1`. It returns true instead of 2, though. * There are some arguments about the syntax "case...in". But if we have ` in `, "case...in" can be considered as a syntactic sugar that is useful for multiple-clause cases, and looks more natural to me. There are two points I should note: * ` in ` is an expression like ` and `, so we cannot write it as an argument: `foo(1 in 1)` causes SyntaxError. You need to write `foo((1 in 1))` as like `foo((1 and 1))`. I think it is impossible to implement. * Incomplete pattern matching also rewrites variables: `[1, 2, 3] in x, 42, z` will write 1 to the variable "x". This behavior is the same as the current "case...in". Nobu wrote a patch: https://github.com/nobu/ruby/pull/new/feature/expr-in-pattern -- https://bugs.ruby-lang.org/ Unsubscribe: