haskell模拟:使用类约束在数据类型中定义func

时间:2018-11-19 12:51:57

标签: haskell

有功能

httpLBS :: MonadIO m => Request -> m (Response ByteString) 

Network.HTTP.Simple 模块(doc)中,我想与我的 Ctx 一起传递(以便稍后在集成测试中进行模拟) >数据对象

data Ctx =
    Ctx {
        token :: String,
        httpLBSFunc :: MonadIO m => Request -> m (Response ByteString)
    } deriving (Show)

但我遇到此错误:

Prelude> :l Context.hs
[1 of 1] Compiling Context          ( Context.hs, interpreted )

Context.hs:19:32: error: Not in scope: type variable ‘m’
   |
19 |         httpLBSFunc :: MonadIO m => Request -> m (Response B.ByteString)
   |                                ^

Context.hs:19:48: error: Not in scope: type variable ‘m’
   |
19 |         httpLBSFunc :: MonadIO m => Request -> m (Response B.ByteString)
   |                                                ^
Failed, no modules loaded.

如何在我的 Ctx 数据类型中正确定义此方法?

1 个答案:

答案 0 :(得分:0)

我没有用实际的Monad IO 代替约束 MonadIO 的缺点,但是对我来说,使其运行就足够了:

data Ctx =
    Ctx {
        token :: String,
        httpLBSFunc :: Request -> IO (Response ByteString)
    } deriving (Show)

我什至没有得到它们之间的确切差异