Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Can you not write any program using either?

While you can make do with either, they have complementary strengths and weaknesses, which are useful in different situations. See "Expression Problem", and "Visitor Pattern".

> Why not?

"Single Dispatch Polymorphism" is a language feature. ADTs/pattern matching are a language feature. Subtyping is a type-system feature. "Structural Typing" is a type-system feature. Guaranteeing exhaustive pattern matching is a type-system feature. They are not "type systems", and different languages pick them and combine them a la carte.

> But case classes cannot be part of an inheritance hierarchy nor structural types (or inheritance types) be used as ADTs

Yes they can, and they do. You just can't extend case classes from other case classes. You can use any class as an ADT-like structure if you want, leaving the hierarchy open or closed, and defining pattern matching extractors as you please. It's all part of the same system; whether you call it an "ADT" or a "class hierarchy" is a design pattern thing more than a rigid systemic property.

> Obviously, and that is why some languages employ the one or the other

Er... that doesn't follow. What I said is that it's very useful having both together. If you don't think that's valuable, that's fine; Scala's not for you.



Can you write any program with only case classes and sealed traits? Yes; Haskell does it. Can you write any program with just structural types? Yes, JS does it. Can you write any program with just inheritance types? Yes, Java, C++ and C# do it. Ergo, Scala has three type systems, and it doesn't matter whether you make them appear as one.

> It's all part of the same system; whether you call it an "ADT" or a "class hierarchy" is a design pattern thing more than a rigid systemic property.

Obviously it's part of the same system, which just happens to be a superset of three type systems. A type system is not how its implemented but what it does.


You can write any program with a Turing machine. That's a silly definition of a type system. I've never heard this usage anywhere else.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: