[#107430] [Ruby master Feature#18566] Merge `io-wait` gem into core IO — "byroot (Jean Boussier)" <noreply@...>
Issue #18566 has been reported by byroot (Jean Boussier).
22 messages
2022/02/02
[ruby-core:107752] [Ruby master Feature#18583] Pattern-matching: API for custom unpacking strategies?
From:
"hmdne (hmdne -)" <noreply@...>
Date:
2022-02-27 03:24:33 UTC
List:
ruby-core #107752
Issue #18583 has been updated by hmdne (hmdne -).
> `# looking into global value isn't exactly elegant, right?`
It's not global, it's Fiber-local, so are $1 and friends. This may not be messaged well enough in the documentation though...
```ruby
[1] pry(main)> z = Fiber.new { /(.)/ =~ 'test' }
=> #<Fiber:0x00007f698a2897e0 (pry):1 (created)>
[2] pry(main)> z.resume
=> 0
[3] pry(main)> Regexp.last_match
=> nil
[4] pry(main)>
```
----------------------------------------
Feature #18583: Pattern-matching: API for custom unpacking strategies?
https://bugs.ruby-lang.org/issues/18583#change-96677
* 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/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>