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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access