首页 > 解决方案 > haskell 模拟:在具有类约束的数据类型中定义 func

问题描述

有功能

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数据类型中正确定义此方法?

标签: haskell

解决方案


我不知道用实际的 Monad IO替换约束MonadIO有什么缺点,但对我来说,让它运行就足够了:

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

我什至不知道它们之间的确切区别


推荐阅读