ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "zverok (Victor Shepelev)" <noreply@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:107565] [Ruby master Feature#18583] Pattern-matching: API for custom unpacking strategies?
Date: Sat, 12 Feb 2022 20:35:09 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-96478.20220212203509.710@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-18583.20220212202253.710@ruby-lang.org

Issue #18583 has been updated by zverok (Victor Shepelev).


One simpler example is, that matching something with regexps with capture groups is still quite annoying!
```ruby
case string
when /{{(.+?)}}/
  content = Regexp.last_match[1] # looking into global value isn't exactly elegant, right?
```

We could've probably bend it towards
```ruby
case string
in /{{(.+?)}}/ => content # the matched group
```

This, though, raises a question of several match groups, at which point one starts to want more:
```ruby
case string
in /{{(.+?): (.+?)}}/ => [key, value]
  # use key and value
in /{{=(?<named>.+?)}}/ => {named:}
  # use named
```
...so... IDK.

----------------------------------------
Feature #18583: Pattern-matching: API for custom unpacking strategies?
https://bugs.ruby-lang.org/issues/18583#change-96478

* Author: zverok (Victor Shepelev)
* Status: Open
* Priority: Normal
----------------------------------------
I started to think about it when discussing https://github.com/ruby/strscan/pull/30. 
The thing is, usage of StringScanner for many complicated parsers invokes some kind of branching.

In pseudocode, the "ideal API" would allow to write something like this:
```ruby
case <what next matches>
in /regexp1/ => value_that_matched
  # use value_that_matched
in /regexp2/ => value_that_matched
  # use value_that_matched
# ...
```
This seems "intuitively" that there *should* be some way of implementing it, but we fall short. We can do some StringScanner-specific matcher object which defines its own `#===` and use it with pinning:
```ruby
case scanner
in ^(Matcher.new(/regexp1/)) => value_that_matched
# ...
```
But there is no API to tell how the match result will be unpacked, just the whole `StringScanner` will be put into `value_that_matched`.

So, I thought that maybe it would be possible to define some kind of API for pattern-like objects, the method with signature like `try_match_pattern(value)`, which by default is implemented like `return value if self === value`, but can be redefined to return something different, like part of the object, or object transformed somehow.

This will open some interesting (if maybe uncanny) possibilities: not just slicing out the necessary part, but something like
```ruby
value => ^(type_caster(Integer)) => int_value
```

So... Just a discussion topic!



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

  reply	other threads:[~2022-02-12 20:35 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-12 20:22 [ruby-core:107564] [Ruby master Feature#18583] Pattern-matching: API for custom unpacking strategies? zverok (Victor Shepelev)
2022-02-12 20:35 ` zverok (Victor Shepelev) [this message]
2022-02-27  3:24 ` [ruby-core:107752] " hmdne (hmdne -)
2022-03-17 13:10 ` [ruby-core:107950] " palkan (Vladimir Dementyev)
2024-04-28 16:11 ` [ruby-core:117735] " ntl (Nathan Ladd) via ruby-core

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-96478.20220212203509.710@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).