4 ms·
Reading about this, I followed a link to this talk advocating pattern matching (using Mach7) over the Visitor pattern: https://www.youtube.com/watch?v=QhJguzpZO
by haberman 11y ago
Reading about this, I followed a link to this talk advocating pattern matching (using Mach7) over the Visitor pattern: https://www.youtube.com/watch?v=QhJguzpZOrk https://www.youtube.com/watch?v=QhJguzpZOrk
This was really interesting to me, because I had never thought of pattern matching and the Visitor pattern as being "competitors", so-to-speak. I guess this would mainly apply in the case that your pattern matching is based on type.
I was further intrigued because this argument (pattern matching is better than Visitor) seems to be the opposite argument made in the Google C++ Style Guide, which I usually agree with. The Google C++ Style Guide argues against using RTTI to do type-based dispatch (https://google-styleguide.googlecode.com/svn/trunk/cppguide.html#Run-Time_Type_Information__RTTI_ https://google-styleguide.googlecode.com/svn/trunk/cppguide....):
"Decision trees based on type are a strong indication that your code is on the wrong track. Code such as this usually breaks when additional subclasses are added to the class hierarchy. Moreover, when properties of a subclass change, it is difficult to find and modify all the affected code segments."
It's very interesting to me that there is such wide disagreement here on which of these two patterns represents better design.
- herewego 11y agoSeems like performance is a real advantage, too. Similar to why we sometimes see static CRTP used for the Visitor pattern. I'm using static CRTP for a project that requires extremely low latency, and when compared to it's virtual counterpart, it delivers.
- jeremyjh 11y agoYeah, its cute but it doesn't provide exhaustiveness testing which is what the Google guide is concerned about. I think Scala came up with a neat way to work that into an OO model with its sealed attribute. A sealed case class can't be extended past its original source file, so its inheritance hierarchy is fixed. Pattern matches on a sealed case class will warn for lack of exhaustiveness just like matches on ADTs do in Haskell, Rust etc.
- Someone 11y agoFor the language history buffs: sealed classes aren't a Scala invention. Dylan had them, too (at the library, rather than the source level. http://opendylan.org/books/dpg/perform.html http://opendylan.org/books/dpg/perform.html) (Better language history buffs than me, feel free to show prior art)
- steveklabnik 11y agoSmall note: non-exhaustive patterns are an error in Rust, not a warning.
- ufo 11y agoDunno if he mentions it on the video but in the FP community they refer to this dicoromy as the "Expression Problem" http://c2.com/cgi/wiki?ExpressionProblem http://c2.com/cgi/wiki?ExpressionProblem The name comes from choosing the best way to represent a datatype for expressions in a programming language. The pattern-matching approach makes it easy to define new functions on top of ASTs and the OO-approach makes it easy to add new kinds of node to the tree but its really hard to make a system that is easy to extend in both directions.