class Bytestringable or ToBytestring

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

Why not use
http://hackage.haskell.org/packages/archive/newtype/0.2/doc/html/Control-New...
instead?
On Thu, 22 Nov 2012 14:15:00 +0000 Vincent Hanquez
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,

On 11/22/2012 03:42 PM, kudah wrote:
Why not use
http://hackage.haskell.org/packages/archive/newtype/0.2/doc/html/Control-New...
instead? interesting i didn't know about it, however it's seems relatively unknown (can't find any library on hackage that use it) and just like Serialize and Binary the interface goes both way, where i'm looking only at the unpack method.
-- Vincent

On Thu, 22 Nov 2012 21:14:31 +0000 Vincent Hanquez
can't find any library on hackage that use it

i recently found the convertible package http://hackage.haskell.org/packages/archive/convertible/1.0.11.1/doc/html/Da... Convertible-Base.html convert :: Convertible a b => a -> b I've only used it once but it looks good to me. sure the type checker does not guartantee that you get a ByteString back but if you only use your own types where you write all the instances yourself you should be safe. silvio
participants (3)
-
kudah
-
Silvio Frischknecht
-
Vincent Hanquez