The fact that ADT are closed makes it a lot easier to write total functions. That are functions that always produce a result, for all possible values of its type, eg.
maybeToList :: Maybe a -> [a]
maybeToList Nothing = []
maybeToList (Just x) = [x]
If Maybe
were open, someone could add a extra constructor and the maybeToList
function would suddenly break.
In OO this isn't an issue, when you're using inheritance to extend a type, because when you call a function for which there is no specific overload, it can just use the implementation for a superclass. I.e., you can call printPerson(Person p)
just fine with a Student
object if Student
is a subclass of Person
.
In Haskell, you would usually use encapsulation and type classes when you need to extent your types. For example:
class Eq a where
(==) :: a -> a -> Bool
instance Eq Bool where
False == False = True
False == True = False
True == False = False
True == True = True
instance Eq a => Eq [a] where
[] == [] = True
(x:xs) == (y:ys) = x == y && xs == ys
_ == _ = False
Now, the ==
function is completely open, you can add your own types by making it an instance of the Eq
class.
Note that there has been work on the idea of extensible datatypes, but that is definitely not part of Haskell yet.