When i was thinking about language design i got an idea that ADTs (Aglebraic Data Types) and typeclasses could be the same thing. They can both represent a group of types, but in haskell they are not the same thing. Typeclass can be extended later, but ADT can't.
But, sometimes typeclass also cant do everything a ADT can. For example:
class Shape a where draw :: a -> IO () data Rectangle = Rectangle Float Float instance Shape Rectangle where draw rectangle = ... -- no way to put constraint for shape data Group a = Group [a] instance Shape Group wheres draw group = ...
With ADT it's simple and working, but no new shape can be added:
data Shape = Rectangle Float Float | Group [Shape] draw (Rectangle x y) = ... draw (Group [ss]) = ...
If classes were more like ADTs, language could behave something like this when it comes to booleans (this is not syntax of a real program):
-- a group of types class Bool -- one type with one constructor data True -- adding type to group instance Bool True data False instance Bool False
This could also be made with numbers, which would look even more rediculous, but i hope u can get my point.
Is it posible that in other language this two things are one? Does any other language work that way? What are the reasons that ADTs and typeclasses should be seperated?