类型签名中的变形金刚?
Transformers in type signature or not?
只是在考虑 API 设计。 Haskell中的"common"是什么?类型签名中的变形金刚或 "hidden"?
findById :: ID -> IO (Maybe User)
findById x = runMaybeT $ do
...
return User
或
findById :: ID -> MaybeT IO User
findById x = do
...
return User
如果这是为了一些简单的事情,并且只有少数函数可以在 IO 中执行此操作,那么我只会将类型设置为 IO (Maybe User)
.
如果这是一个跨越你的库的模式,我会给 tfm-stack monad 一个半抽象的名字:
type Request = MaybeT IO
findById :: ID -> Request User
...甚至
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
newtype Request a = Request (runRequest :: MaybeT IO a)
deriving (Functor, Applicative, Monad)
签名 ID -> MaybeT IO User
不是很好:转换器只有在你在那个 monad 中做一大堆动作时才有用,但在那种情况下总是写出 MaybeT IO
违反DRY 原则。
只是在考虑 API 设计。 Haskell中的"common"是什么?类型签名中的变形金刚或 "hidden"?
findById :: ID -> IO (Maybe User)
findById x = runMaybeT $ do
...
return User
或
findById :: ID -> MaybeT IO User
findById x = do
...
return User
如果这是为了一些简单的事情,并且只有少数函数可以在 IO 中执行此操作,那么我只会将类型设置为 IO (Maybe User)
.
如果这是一个跨越你的库的模式,我会给 tfm-stack monad 一个半抽象的名字:
type Request = MaybeT IO
findById :: ID -> Request User
...甚至
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
newtype Request a = Request (runRequest :: MaybeT IO a)
deriving (Functor, Applicative, Monad)
签名 ID -> MaybeT IO User
不是很好:转换器只有在你在那个 monad 中做一大堆动作时才有用,但在那种情况下总是写出 MaybeT IO
违反DRY 原则。