4 ms·
The way it goes is that sum types are closed for extension, while enjoying the confort of pattern matching, whereas OOP approach is open for extension, with the
by pjmlp 1y ago
The way it goes is that sum types are closed for extension, while enjoying the confort of pattern matching, whereas OOP approach is open for extension, with the associated boilerplate in the visitor logic.
In languages of ML linage, we can combine both approaches with a mix of sum types and functors/type classes.
- eru 1y ago> [...] whereas OOP approach is open for extension, with the associated boilerplate in the visitor logic. Yes, by default. But doesn't eg Java let you mark classes as final or something like that? > In languages of ML linage, we can combine both approaches with a mix of sum types and functors/type classes. I think Rust allows something similar, but I'm not sure whether you'd call it 'ML linage'?
- pjmlp 1y agoYes, you can mark classes final as closed for extension, however Java follows the wisdom at the time that by default classes are open for extension. Additionally, modern Java has sum types modeled on the same way as Scala. Rust is to some extent inspired by ML languages, hence why several OCaml and Haskell refugees also hang around Rust.