Haskell ByteString / Data.Binary.Get 问题
尝试使用 Data.Binary.Get 和 ByteString 但不明白发生了什么。我的代码如下:
getSegmentParams :: Get (Int, L.ByteString)
getSegmentParams = do
seglen <- liftM fromIntegral getWord16be
params <- getByteString (seglen - 2)
return (seglen, params)
对于返回元组的第三项,即有效负载,我收到以下错误:
Couldn't match expected type `L.ByteString'
against inferred type `bytestring-0.9.1.4:Data.ByteString.Internal.ByteString'
有人请向我解释 Data.Binary.Get 和 ByteStrings 之间的交互以及我如何能够做我想要的事情。谢谢。
Attempting to use Data.Binary.Get and ByteString and not understanding what's happening. My code is below:
getSegmentParams :: Get (Int, L.ByteString)
getSegmentParams = do
seglen <- liftM fromIntegral getWord16be
params <- getByteString (seglen - 2)
return (seglen, params)
I get the following error against the third item of the return tuple, ie payload:
Couldn't match expected type `L.ByteString'
against inferred type `bytestring-0.9.1.4:Data.ByteString.Internal.ByteString'
Someone please explain to me the interaction between Data.Binary.Get and ByteStrings and how I can do what I'm intending. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它表示您希望元组的第二个元素是
L.ByteString
(我假设 L 来自Data.ByteString.Lazy
),但是getByteString< /code> 例程从
Data.ByteString
返回严格的 ByteString。您可能想使用getLazyByteString
。It says you expect the second element of the tuple to be a
L.ByteString
(I assume that L is fromData.ByteString.Lazy
) but thegetByteString
routine returns a strict ByteString fromData.ByteString
. You probably want to usegetLazyByteString
.有两种
ByteString
数据类型:一种位于Data.ByteString.Lazy
中,一种位于Data.ByteString
中。考虑到
L
限定了您的 ByteString,我认为您想要的是惰性类型,但是getByteString
为您提供了严格的ByteString
。惰性
ByteString
在内部由严格ByteString
列表表示。幸运的是,Data.ByteString.Lazy 为您提供了一种将严格的 ByteString 列表转换为惰性 ByteString 的机制。
如果您定义,
您可以将代码片段更改为,
并且一切都应该与世界正确。
There are two
ByteString
data types: one is inData.ByteString.Lazy
and one is inData.ByteString
.Given the
L
qualifying your ByteString, I presume you want the lazy variety, butgetByteString
is giving you a strictByteString
.Lazy
ByteString
s are internally represented by a list of strictByteString
s.Fortunately
Data.ByteString.Lazy
gives you a mechanism for turning a list of strictByteString
s into a lazyByteString
.If you define
you can change your code fragment to
and all should be right with the world.