Algebraic Data Types (ADTs) in Haskell can automatically become instances of some typeclasses (like Show
, Eq
) by deriving from them.
data Maybe a = Nothing | Just a
deriving (Eq, Ord)
My question is, how does this deriving
work, i.e. how does Haskell know how to implement the functions of the derived typeclass for the deriving ADT?
Also, why is deriving
restricted to certain typeclasses only? Why can't I write my own typeclass which can be derived?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…