Project

General

Profile

Actions

Feature #17406

open

Add `NoMatchingPatternError#depth`

Added by marcandre (Marc-Andre Lafortune) almost 4 years ago. Updated almost 4 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:101527]

Description

Could we have NoMatchingPatternError#depth, returning the number of case...in...end an exception has traversed?

def show_depth
  yield
rescue NoMatchingPatternError => e
  puts "Depth: #{e.depth}"
  raise
end

show_depth do
  case [1,2,3]
  in [x, y, z] then
    show_depth do
      x => [a, b] # => raises NoMatchingPatternError
    end
  end
end

# Prints "Depth: 0" then "Depth: 1"

This could help bring pattern match closer to a language construct people can play with.

Example usecase: implement Ractor#receive_if as in https://bugs.ruby-lang.org/issues/17378#note-6

Updated by Eregon (Benoit Daloze) almost 4 years ago

How would this work?
Mutating a field of the NoMatchingPatternError in a catch(NoMatchingPatternError) inserted in every in and else?
Seems rather hacky to me.

Updated by marcandre (Marc-Andre Lafortune) almost 4 years ago

Eregon (Benoit Daloze) wrote in #note-1:

How would this work?
Mutating a field of the NoMatchingPatternError in a catch(NoMatchingPatternError) inserted in every in and else?

I think of it as a catch in each case-in, but the effect is the same.

Seems rather hacky to me.

Possibly 😅.
Actual downside?

Actions

Also available in: Atom PDF

Like0
Like0Like0