Is it safe to derive MonadThrow, MonadCatch, MonadBaseControl, MonadUnliftIO, etc?

前端 未结 1 1192
长发绾君心
长发绾君心 2021-01-24 08:15

I\'m refactoring some old code, which is in a polymorphic, but type-class constrained, monad:

class ( MonadIO m
      , MonadLogger m
      , MonadLoggerIO m
            


        
1条回答
  •  闹比i
    闹比i (楼主)
    2021-01-24 08:44

    The user manual has documentation about every extension, and it keeps getting better; here's the section on deriving, that should be sufficient to know what's actually happening: https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html#extensions-to-the-deriving-mechanism

    In this case, all those classes are handled by GeneralizedNewtypeDeriving.

    {-# LANGUAGE GeneralizedNewtypeDeriving, UndecidableInstances #-}
    
    module M where
    
    import Control.Monad.IO.Unlift
    import Control.Monad.Catch
    import Control.Monad.Trans.Control
    import Control.Monad.Base
    import Control.Monad.Reader
    
    newtype Foo a = Foo (ReaderT () IO a)
      deriving (Functor, Applicative, Monad, MonadIO, MonadUnliftIO, MonadThrow, MonadCatch, MonadMask, MonadBase IO, MonadBaseControl IO)
    

    In general, the three relevant extensions for user-defined classes are GeneralizedNewtypeDeriving, DerivingVia, and DeriveAnyType. And it's also worth enabling DerivingStrategies to make it explicit which is being used.

    0 讨论(0)
提交回复
热议问题