Changes between Version 2 and Version 3 of NewtypeDeriving


Ignore:
Timestamp:
Dec 6, 2005 8:04:31 AM (9 years ago)
Author:
john@…
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • NewtypeDeriving

    v2 v3  
    88The `deriving` clause on a `newtype` can list any class.  For any class except `Read`, `Show`, `Typeable` or `Data`, the instance is "the same" as for the wrapped type. 
    99 
     10== Examples == 
     11 
     12one area where this is particularly useful is when dealing with monad transformers. for example the jhc typechecking monad is declared as follows 
     13{{{ 
     14data TcEnv = TcEnv { ... } 
     15 
     16newtype TI a = TI (ReaderT TcEnv IO a) 
     17    deriving(Monad,MonadFix,MonadIO,MonadReader TcEnv,Functor)  
     18}}} 
     19and we end up with a complete monad with every interesting instance defined 
     20for free.  
     21 
     22It is also good for selectively hiding properties of monad transformers 
     23 
     24{{{ 
     25-- | Unique integer generator monad transformer. 
     26newtype UniqT m a = UniqT (StateT Int m a) 
     27    deriving(Monad,MonadTrans, Functor, MonadFix, MonadPlus)   
     28}}} 
     29 
     30notice that while it uses a state transformer internally, the MonadState class 
     31is intentionally left out of the deriving clause. this means that calls to the 
     32state monad will pass through the UniqT making stacking monads signifigantly 
     33easier. 
     34 
    1035== References == 
    1136 * [http://www.haskell.org/ghc/docs/latest/html/users_guide/type-extensions.html#newtype-deriving  GHC documentation]