conduit和network-conduit:结合MonadResource和IO

时间:2013-01-12 11:09:05

标签: haskell conduit

我正在尝试使用导管包。我还找到了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")

然而,这不起作用,因为app sink不属于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中而不实际管理资源。但我无法弄清楚如何做到这一点。

...

1 个答案:

答案 0 :(得分:5)

自己搞清楚...只需将整个runTCPClient包装到runResourceT中。