Wrapping Types with Newtype
One of the key limitations that you’ve seen when working with type classes is that each data type must have at most a single instance of the type class. Having more than one implementation of show or read for a type would not only make it impossible for the compiler to select an implementation to use, it would also make the program unmaintainably confusing as the intention behind what a type class means for any given type would be obscured. As with the MyResult type that you built in the last section, which was structurally identical to Either, it’s common that you might want to have some underlying data structure represented by more than one type, each with its own name and perhaps its own type class instances as ...
Get Effective Haskell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.