Generalized newtype deriving

Contents
  • Example: Error
  • Example: Sum and Product

When you define a type using the newtype keyword,

you are defining a new type X is representationally equivalent to Y, but nominally different; although X and Y have the same representation, they are different types. This means that they can have different typeclass instances.

Often, however, you want some (or all) of the typeclass instances for a newtype to be exactly the same as the instances of the underlying type. The GeneralizedNewtypeDeriving GHC documentation for GeneralizedNewtypeDeriving. GHC extension provides a convenient way to do that.

As of [GHC release 8.6.1] GHC 8.6.1 release notes, you can now spell this extension GeneralisedNewtypeDeriving if you prefer. We will stick with the American spelling in this article.

Sign up for access to the full page, plus the complete archive and all the latest content.