Project

General

Profile

Actions

Feature #15918

open

Pattern matching for Set

Added by marcandre (Marc-Andre Lafortune) over 5 years ago. Updated 7 months ago.

Status:
Assigned
Target version:
-
[ruby-core:93075]

Description

Currently, Set does not respond to deconstruct. Shouldn't we implement it using to_a?

require 'set'
case Set[1, 2, 3]
in [1, 2, 3]
  p "match"
else
  p "no match"
end
# => "no match", should be "match"

Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #14912: Introduce pattern matching syntaxClosedktsj (Kazuki Tsujimoto)Actions

Updated by Eregon (Benoit Daloze) over 5 years ago

Did you mean in Set[1, 2, 3] ?

Updated by marcandre (Marc-Andre Lafortune) over 5 years ago

Eregon (Benoit Daloze) wrote:

Did you mean in Set[1, 2, 3] ?

I didn't, but it should match too; it's the same as my example but with the added constraint that the object should be a descendant a Set. Note that in Set[1, 2, 3] does not call Set.[](1, 2, 3)...

Updated by jeremyevans0 (Jeremy Evans) over 5 years ago

Sets are supposed to be unordered (any ordering is an implementation detail). If Set[1, 2, 3] matches in your example, so should Set[3, 2, 1], since Set[1, 2, 3] == Set[3, 2, 1]. We could attempt to sort the elements of the set before pattern matching, but some sets contain unsortable elements (e.g. elements of different types). If pattern matching can work correctly when using in Set[...], then maybe this would be desirable, but I'm not sure if that is possible.

To answer your question, in my opinion, yes, we shouldn't implement deconstruct using to_a.

Actions #4

Updated by jeremyevans0 (Jeremy Evans) over 5 years ago

  • Tracker changed from Bug to Feature
  • Backport deleted (2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN)
Actions #5

Updated by ktsj (Kazuki Tsujimoto) over 5 years ago

Updated by ko1 (Koichi Sasada) over 5 years ago

  • Assignee set to ktsj (Kazuki Tsujimoto)
Actions #7

Updated by hsbt (Hiroshi SHIBATA) 7 months ago

  • Status changed from Open to Assigned
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0