Generalized newtype deriving

  • Example: Error
  • Example: Sum and Product
  • History

When you define a type using the newtype keyword,

newtype X = X Y

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 typeclass instances for a newtype that are exactly the same as the instances of the underlying type. The GeneralizedNewtypeDeriving GHC extension provides a convenient way to do that.

Generalized newtype derivingGHC documentation for GeneralizedNewtypeDeriving has been around since GHC 5.04, with a slight refinement in GHC 7.8.1.

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