派生MonadThrow,MonadCatch,MonadBaseControl,MonadUnliftIO等是否安全? [英] Is it safe to derive MonadThrow, MonadCatch, MonadBaseControl, MonadUnliftIO, etc?

查看:119
本文介绍了派生MonadThrow,MonadCatch,MonadBaseControl,MonadUnliftIO等是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重构一些旧代码,这些代码是多态的,但受类型类约束的monad:

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

class ( MonadIO m
      , MonadLogger m
      , MonadLoggerIO m
      , MonadThrow m
      , MonadCatch m
      , MonadMask m
      , MonadBaseControl IO m
      , MonadUnliftIO) => HasLogging m where

在较旧的代码中,应用程序的主要monad是...

In the older code the application's main monad was...

type AppM = ReaderT Env IO

...现在将更改为...

...which will now change to...

newtype AppM (features :: [FeatureFlag]) a = AppM (ReaderT Env IO a)
  deriving (Functor, Applicative, Monad, MonadReader Env, MonadIO)

在这种情况下,自动导出以下内容是否安全:

Given this context, is it safe to derive the following, automatically:

  • MonadThrow
  • MonadCatch
  • MonadMask
  • MonadBaseControl
  • MonadUliftIO

无需深入了解GHC内部原理,当编译器自动地推导事物时,对直觉发生的事情有直觉的最佳方法是什么?

Without getting into GHC internals, what's the best way to develop intuition about what's actually happening when the compiler derives things automagically?

推荐答案

该用户手册包含有关每个扩展的文档,并且文档会不断完善.这是有关派生的部分,应该足以了解实际发生的情况:

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

在这种情况下,所有这些类均由GeneralizedNewtypeDeriving处理.

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)

通常,用户定义类的三个相关扩展是GeneralizedNewtypeDerivingDerivingViaDeriveAnyType.而且还值得启用DerivingStrategies使其明确显示正在使用的内容.

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.

这篇关于派生MonadThrow,MonadCatch,MonadBaseControl,MonadUnliftIO等是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆