[Haskell-cafe] Compatibility between Data.ByteString.Base and Data.ByteString.Lazy

Donald Bruce Stewart dons at cse.unsw.edu.au
Wed Nov 29 08:21:14 EST 2006


neil:
> Hi,
> 
> Firstly my apologies if this is an outrageously newbie question.
> 
> I am trying to write a binary protocol parser using Data.ByteString. I
> have created a module "ByteParser" containing my parsing utilities, which
> imports ByteString as:
> 
>   import qualified Data.ByteString as B
> 
> In my Main module, where all the IO happens, I want to use lazy
> ByteStrings so I ask for the following imports:
> 
>   import ByteParser
>   import qualified Data.ByteString.Lazy as L
> 
> The problem is that when I try to call a function in ByteParser with an
> L.ByteString, the compiler complains that:
> 
>   Couldn't match expected type `Data.ByteString.Base.ByteString'
>   against inferred type `L.ByteString'
> 
> Does this mean that the lazy version of ByteString is a completely
> separate, incompatible type from the base version? Obviously I can work

Yes, they're completely different. One is a strict, unboxed array of
bytes. The other is a lazy list of cache-sized array chunks.

You can convert between the two. But usually its best to pick one and
stick to it.

> around this problem by making ByteParser import Data.ByteString.Lazy
> instead of Data.ByteString, but then ByteParser would not be able to work
> with strict ByteStrings. Ideally I would like ByteParser to be agnostic
> about the use of lazy versus strict ByteStrings. Is this a sensible and/or
> possible thing to do?

You could make your binary parser polymorphic on the string type, and
used a String class that both bytestrings are instances of.

-- Don


More information about the Haskell-Cafe mailing list