|
195 | 195 | -- > instance NFData A where rnf = grnf |
196 | 196 | -- > instance NFData a => NFData (B a) where rnf = grnf |
197 | 197 | -- |
| 198 | +-- == Deriving Generic |
| 199 | +-- |
| 200 | +-- The 'Generic' class can also be derived in two ways |
| 201 | +-- (this uses @-XDerivingStrategies@ for more clarity) |
| 202 | +-- |
| 203 | +-- === Using @-XDeriveAnyClass@ |
| 204 | +-- |
| 205 | +-- > {-# LANGUAGE DeriveGeneric, DerivingStrategies, DeriveAnyClass #-} |
| 206 | +-- > |
| 207 | +-- > import qualified GHC.Generics as GHC |
| 208 | +-- > import Generics.SOP |
| 209 | +-- > data A = B Bool | C Int |
| 210 | +-- > deriving stock GHC.Generic |
| 211 | +-- > deriving anyclass Generic |
| 212 | +-- |
| 213 | +-- === Using @-XDerivingVia@ (GHC versions greater or equal 8.6.1) |
| 214 | +-- |
| 215 | +-- > {-# LANGUAGE DeriveGeneric, DerivingStrategies, DerivingVia #-} |
| 216 | +-- > |
| 217 | +-- > import qualified GHC.Generics as GHC |
| 218 | +-- > import Generics.SOP |
| 219 | +-- > import GHC.Generics (Generically (Generically)) |
| 220 | +-- > data A = B Bool | C Int |
| 221 | +-- > deriving stock GHC.Generic |
| 222 | +-- > deriving Generic via Generically A |
| 223 | +-- |
| 224 | +-- In the @-XDerivingVia@ case be careful to |
| 225 | +-- |
| 226 | +-- 1. import the constructor of 'Generically' |
| 227 | +-- 2. import the correct module i.e. on base versions older than 4.17 which was |
| 228 | +-- first shipped with GHC 9.4.1 @import GHC.Generics.Generically (Generically (Generically))@ |
| 229 | +-- from package [@generically@](https://hackage.haskell.org/package/generically) |
| 230 | +-- |
198 | 231 | -- = More examples |
199 | 232 | -- |
200 | 233 | -- The best way to learn about how to define generic functions in the SOP style |
|
0 commit comments