Issue #14709 has been updated by shevegen (Robert A. Heiler).


zverok wrote an add-on in regards to "syntax proposal" here:

https://zverok.github.io/blog/2018-06-26-pattern-matching.html

I don't have a suggestion for a better syntax myself really.

zverok suggested, among other examples, e. g. this:

    case (lat, *lng)
    when (Numeric, Numeric => lng, Hash => opts)

I am not a huge fan of the (). :P

However had, I was also thinking ... pattern matching is in
some ways similar to regex-checking, yes? For regexes we 
have =~.

Perhaps we could use a similar construct for pattern matching,
also starting with =. Not sure about the second character.

=| might be almost an obvious choice since it looks like a
pipe in a way, but I am not sure if this can be used.

One could also ponder about a combination of three characters,
a bit like the somewhat new one used for lambdas. ->| or
->~  ... not that any of these are very pretty. And one thing
I also always mentioned is that it should not be too hard to
visually differentiate between different things in ruby code
so perhaps something like ->~ is a bad idea since it is not
so easy to see any difference. But two characters may be fine,
like the regex variant or also the compound ones like += or
-= (two characters seem better than more than two characters).

----------------------------------------
Feature #14709: Proper pattern matching
https://bugs.ruby-lang.org/issues/14709#change-72673

* Author: zverok (Victor Shepelev)
* Status: Closed
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
On RubyKaigi 2017, there was a [presentation](http://rubykaigi.org/2017/presentations/yotii23.html) of Yuki Torii about possible implementation of pattern matching.

The syntax proposed in presentation was:

```ruby
res = [:ng, 500]
case res
when %p([:ng, status])
  p status
end
```

The proposed syntax seem to feel pretty consistent, and the implementation (forked Ruby interpreter) was working at this moment.

As @ko1 was one of the contributors to the experiment, I don't suppose Ruby core team is not aware of the proposal, so I'd like to know what the status of it? Are there some plans for full-strength pattern matching in Ruby 3 (with proposed, or any other, syntax)?

PS: There are many existing gems with some kind "almost real" pattern matching (including recently emerged [Qo](https://github.com/baweaver/qo)), yet I believe that the _only_ useful pattern matching can be provided language core. Otherwise, two main goals can't be achieved:
* reasonable performance (as the pattern-matching is useful mostly in complicated algorithms, which tend to repeat matches thousands of times);
* unpacking of parts of the patterns into local variables.



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>