【问题标题】:conduit and network-conduit: Combine MonadResource and IO管道和网络管道:结合 MonadResource 和 IO
【发布时间】:2013-01-12 11:09:05
【问题描述】:

我正在试验导管包。还找到了 network-conduit 包,我尝试制作一个简单的 TCP 客户端,它将文件的内容发送到套接字:

import Data.Conduit
import Data.Conduit.Binary
import Data.Conduit.Network
import Data.ByteString.Char8 (pack)

sendFile fileName appData = runResourceT $ 
   sourceFile fileName $$ appSink appData

main = runTCPClient (clientSettings 8000 (pack "localhost")) (sendFile "book.tex")

但是,这不起作用,因为应用接收器不存在于 ResourceT:

[1 of 1] Compiling Main             ( Conduit2.hs, interpreted )

Conduit2.hs:9:63:
    Occurs check: cannot construct the infinite type: m0 = ResourceT m0
    Expected type: Application (ResourceT m0)
      Actual type: AppData (ResourceT m0) -> m0 ()
    In the return type of a call of `sendFile'
    In the second argument of `runTCPClient', namely
      `(sendFile "book.tex")'
Failed, modules loaded: none.

但是,如果没有 runResourceT,它显然也无法工作:

[1 of 1] Compiling Main             ( Conduit2.hs, interpreted )

Conduit2.hs:9:63:
    No instance for (MonadResource IO)
      arising from a use of `sendFile'
...etc...

我的猜测是我应该在不实际管理资源的情况下将 appSink(其中 m=IO)包装到 ResourceT 中。但我就是不知道该怎么做。

...?

【问题讨论】:

    标签: haskell conduit


    【解决方案1】:

    我自己想出来了...只需将整个 runTCPClient 包装到 runResourceT 中。

    【讨论】:

    • 既然你发现它应该接受你的答案那么正确。这样,它将在 SO 的搜索中显示为具有可接受的答案。
    猜你喜欢
    • 1970-01-01
    • 2020-05-08
    • 2015-01-18
    • 1970-01-01
    • 2016-08-11
    • 2014-08-20
    • 1970-01-01
    • 2022-11-20
    • 2013-03-14
    相关资源
    最近更新 更多