Re:Pattern Matching in Ruby 2.7 (2019)
by zelly on Thursday January 09, 2020 @02:44AM
(#21999270)
Why can't pattern matching fit in OOP? It's not really functional, despite being in most functional languages. It's just sugar for more lengthy imperative conditionals and unpacking.
[ Parent
| on HN ]
It's typical in statically typed functional languages to match against Algebraic data type value constructors. These ADTs have a fixed amount of variants, which makes it suitable to do a clean pattern match.
ADTs don't exist in OOP. It gets more complicated to do matches against classes whose abstractions are not really decomposable so easily.
Indeed, Objective Caml is a multi-paradigm language with pattern matching.
Re:Pattern Matching in Ruby 2.7 (2019)
by totalperspectiv on Thursday January 09, 2020 @09:11AM
(#22001281)
And Scala. The poster child of oop + functional
Re:Pattern Matching in Ruby 2.7 (2019)
by agumonkey on Thursday January 09, 2020 @06:27AM
(#22000285)
Because part of the OOP mindset is to never dig into an object and only interact with a flat interface. From the few that I remember from OO articles in the 00s.
Now technically yes all FP idioms are just simple operations below that can be used. And they are.. python, js have destructuring which is a first stage pattern matching.
Re:Pattern Matching in Ruby 2.7 (2019)
by zozbot234 on Thursday January 09, 2020 @06:43AM
(#22000362)
Destructuring ("digging into" a type) makes little sense with open records and sums, which OOP introduced into common use. The "closed" ('final', 'sealed' etc.) case where destructuring makes sense was disregarded for some time because it was thought that extensibility was always the appropriate choice.
Today, "open" extensibility mechanisms are considered a bit problematic because it's never really clear what invariants they should preserve, so maintaining correctness is an issue. A "closed" interface makes it essier to specify related invariants.