[Haskell-cafe] class Bytestringable or ToBytestring

Vincent Hanquez tab at snarc.org
Thu Nov 22 15:15:00 CET 2012


Hi cafe,

I've been adding lots of types recently that looks more or less like:

     newtype A = A ByteString
     data B = B ByteString

This is great for extra type safety and letting the compiler do its job, 
however getting the bytestring back requires boiler plate.
At the moment either you give access to the constructor, which is not 
always wanted, or you use the record syntax to create a function to 
extract just the bytestring.
The latter is fine for 1 or 2 types, but the scheme fall apart when 
having many of those types and do pollute namespace.

I'm basically after something that looks like this:

     class ToByteString a where
       toByteString :: a -> ByteString

Before anyone suggest the Serialize interface from cereal or the Binary 
interface from binary which both looks quite similar (from far away):

- serialize work in the put monad, and you have to define a get 
instance: which is something that is not required or possible sometime.
- binary works with lazy bytestrings and got the same problem as cereal.
- a serialize instance that just do a single putByteString is really 
slow: 12 ns to 329 ns (26x time slower) on the same exact data on one 
isolated bench)
- neither of those packages are in the platform.

If that doesn't exists, could it be a worthy addition to bytestring ?
is this a good idea in general ?
is there any other way ?

Thanks,
-- 
Vincent



More information about the Haskell-Cafe mailing list