関連する質問 - 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)
-- | ^^^^^^^^^^^^^
注:>>=
についての最初のエラーは、 についてのエラーとは何の関係もないことに気付きました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)
-- | ^^^^^^^^^^^^^
--