相关问题 -派生 MonadThrow、MonadCatch、MonadBaseControl、MonadUnliftIO 等是否安全?- 我已经启用了 -DeriveAnyClass
并GeneralizedNewtypeDeriving
让代码编译,但没有费心查看不祥的警告。现在,我正在运行重构的代码,它引发了运行时错误:
No instance nor default method for class operation >>=
所以,我只删除DeriveAnyClass
并保留GeneralizedNewtypeDeriving
了以下编译错误:
{-# LANGUAGE DataKinds, GADTs, ScopedTypeVariables, TypeFamilies, AllowAmbiguousTypes, RankNTypes, StandaloneDeriving, UndecidableInstances #-}
newtype AuthM (fs :: [FeatureFlag]) auth m a =
AuthM (ReaderT (Auth auth) m a)
deriving (Functor, Applicative, Monad, MonadReader (Auth auth), MonadIO, MonadThrow, MonadCatch, MonadMask, MonadUnliftIO)
-- • Couldn't match representation of type ‘m (Control.Monad.IO.Unlift.UnliftIO
-- (AuthM fs auth m))’
-- with that of ‘m (Control.Monad.IO.Unlift.UnliftIO
-- (ReaderT (Auth auth) m))’
-- arising from the coercion of the method ‘Control.Monad.IO.Unlift.askUnliftIO’
-- from type ‘ReaderT
-- (Auth auth)
-- m
-- (Control.Monad.IO.Unlift.UnliftIO (ReaderT (Auth auth) m))’
-- to type ‘AuthM
-- fs auth m (Control.Monad.IO.Unlift.UnliftIO (AuthM fs auth m))’
-- NB: We cannot know what roles the parameters to ‘m’ have;
-- we must assume that the role is nominal
-- • When deriving the instance for (MonadUnliftIO (AuthM fs auth m))
-- |
-- 82 | deriving (Functor, Applicative, Monad, MonadReader (Auth auth), MonadIO, MonadThrow, MonadCatch, MonadMask, MonadUnliftIO)
-- | ^^^^^^^^^^^^^
注意:我意识到第一个错误 about>>=
与错误 about 无关MonadUnliftIO
。我已确认关闭>>=
时没有关于丢失的警告。DeriveAnyClass
我想我需要为MonadUnliftIO
自己编写实例,因为编译器可能无法在存在newtype
AND 幻像类型变量的情况下解决这个问题。但是,我只是不知道如何askUnliftIO
为我的类型定义上面给出的。
在最小的代码段尝试 1
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Try13 where
import Control.Monad.Reader
import UnliftIO
import Control.Monad.Catch
data Auth = Auth
newtype AuhM m a = AuthM (ReaderT Auth m a)
deriving(Functor, Applicative, Monad, MonadReader Auth, MonadIO, MonadThrow, MonadCatch, MonadMask, MonadUnliftIO)
-- • Couldn't match representation of type ‘m (UnliftIO (AuhM m))’
-- with that of ‘m (UnliftIO (ReaderT Auth m))’
-- arising from the coercion of the method ‘askUnliftIO’
-- from type ‘ReaderT Auth m (UnliftIO (ReaderT Auth m))’
-- to type ‘AuhM m (UnliftIO (AuhM m))’
-- NB: We cannot know what roles the parameters to ‘m’ have;
-- we must assume that the role is nominal
-- • When deriving the instance for (MonadUnliftIO (AuhM m))
-- |
-- 12 | deriving(Functor, Applicative, Monad, MonadReader Auth, MonadIO, MonadThrow, MonadCatch, MonadMask, MonadUnliftIO)
-- | ^^^^^^^^^^^^^
--