Generalized newtype deriving doesn't work on non-newtypes [GHC-10333]
Language extension: GeneralizedNewtypeDeriving, DerivingStrategies
Generalized newtype deriving implements typeclass derivation for newtypes by coercing them to and from their underlying datatypes. But types that aren’t declared with newtype
cannot be coerced, so other deriving strategies must be used to derive instances for them.
Examples
Attempt to use newtype deriving for an ordinary datatype
Message
DeriveNewtypeData.hs:5:20: error: [GHC-10333]
• Can't make a derived instance of
‘Show Weekend’ with the newtype strategy:
GeneralizedNewtypeDeriving cannot be used on non-newtypes
• In the data declaration for ‘Weekend’
|
5 | deriving newtype Show
| ^^^^
Explanation
Generalized newtype deriving works only on types defined with newtype
, but Weekend
is defined with data
. Ordinary deriving, however, works with types defined with data
.
DeriveNewtypeData.hs
Before
{-# LANGUAGE DerivingStrategies #-}
module DeriveNewtypeData where
data Weekend = Saturday | Sunday
deriving newtype Show
After
module DeriveNewtypeData where
data Weekend = Saturday | Sunday
deriving Show