2014-01-14 42 views
3

此代码不进行类型检查:字节字符串预期不同的字节串

import Network.HTTP.Conduit 
import qualified Data.ByteString.Char8 as BS 

main :: IO() 
main = do 
    resp <- simpleHttp "http://www.google.com" 
    putStrLn $ BS.unpack resp 

抛出以下错误:

Couldn't match expected type `BS.ByteString' 
      with actual type `Data.ByteString.Lazy.Internal.ByteString' 
In the first argument of `BS.unpack', namely `resp' 
In the second argument of `($)', namely `BS.unpack resp' 
In a stmt of a 'do' block: putStrLn $ BS.unpack resp 
Failed, modules loaded: none. 

如何解决这一问题?更改为其他ByteString变体不起作用。

simpleHttp函数的类型是这样的:simpleHttp :: Control.Monad.IO.Class.MonadIO m => String -> m Data.ByteString.Lazy.Internal.ByteString。所以我尝试获取IO monad中的ByteString并尝试使用unpack,但这会导致错误。

+0

什么是进口限定的Data.ByteString.Lazy作为BS'? – viorior

+0

@actionior它引发一个不同类型的错误。 – Sibi

回答

3

有两个单独的ByteString模块,一个用于懒惰的ByteStrings,另一个用于严格的ByteStrings。 simpleHTTP返回一个懒惰的字节串,但是你导入了严格的bytestring模块,所以unpack期待一个严格的字节串。

尝试改变

import qualified Data.ByteString.Char8 as BS 

import qualified Data.ByteString.Lazy.Char8 as BS 

这就是说,你需要的,如果你使用的字节字符串模块的CHAR8版本,因为字符串<要小心 - >字节串转换仅在使用ASCII编码时才有效。我会建议converting your bytestrings to Text与适当的编码功能,然后打印。

+0

将其更改为'Lazy'会引发另一个错误:“无法与'Char'匹配'GHC.Word.Word8'类型 预期类型:字符串 实际类型:[GHC.Word.Word8] 在返回类型'BS.unpack'的调用 在'($)'的第二个参数中,即'BS.unpack resp' 在'do'块的标记中:putStrLn $ BS.unpack resp Failed,modules loaded : 没有。” – Sibi

+0

@Sibi:word8的东西与使用模块的char8版本有关。看我的编辑。 – hugomg