From: jgaskins@... Date: 2018-04-27T02:01:56+00:00 Subject: [ruby-core:86706] [Ruby trunk Feature#14709] Proper pattern matching Issue #14709 has been updated by jgaskins (Jamie Gaskins). Whenever I feel I need pattern matching, I'm trying to handle method args. With some of the ideas presented here, like Qo, I feel I'd really only be using it inside the outer edges of a method to match on the args that that method received �����that is, I'd have to manually pipe the args I received into the pattern matcher every time. I automated that concept in one of my apps, from which I extracted a gem called [method_pattern](https://github.com/jgaskins/method_pattern) (it very slightly predates Qo). The idea behind it, following baweaver's `Net::HTTP` example: ```ruby class Client extend MethodPattern defn :get do with(String) { |url| request(URI(url)) } with(URI) { |uri| handle_response Net::HTTP.get_response(uri) } end defn :handle_response do with(Net::HTTPOK) { |response| JSON.parse(response.body) } with(Net::HTTPNotFound) { raise ArgumentError, "URL doesn't point to a valid resource" } end end ``` This example is only based on classes, but it also works for values because it's based on the `===` method, as well ��� it really is such a great tool for pattern matching. Another example here, for a method `def get(status:, headers:, body:)`, which also shows how handling keyword args can look (even when matching only a subset of them), and even matching nested hashes as in Yuki Torii's `pmruby`: ```ruby class Client extend MethodPattern defn :get do with(status: 200...400, headers: { 'Content-Type' => /json/ }) do |body:, **| handle_success JSON.parse(body, symbolize_names: true) end with(status: 400...500, headers: { 'Content-Type' => /json/ }) do |body:, **| handle_client_error JSON.parse(body, symbolize_names: true) end with(status: 500..599) do |body:, **| handle_server_error body end end end ``` I don't know what first-class syntax could look like for this, unfortunately, but I imagine it might look similar to whatever ideas people had for type annotations. The hard part is making it work with multiple entries per method. ---------------------------------------- Feature #14709: Proper pattern matching https://bugs.ruby-lang.org/issues/14709#change-71664 * Author: zverok (Victor Shepelev) * Status: Open * 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: