繁体   English   中英

管道和网络导管:结合MonadResource和IO

[英]conduit and network-conduit: Combine MonadResource and IO

我正在试验导管套件。 还找到了网络导管包,我尝试制作一个简单的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中,而无需实际管理资源。 但是我只是不知道该怎么做。

...?

我自己弄清楚了...只需要将整个runTCPClient包装到runResourceT中即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM