Vincent Hanquez | 22 Nov 15:15 2012

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.
(Continue reading)

kudah | 22 Nov 16:42 2012
Picon

Re: class Bytestringable or ToBytestring

Why not use

http://hackage.haskell.org/packages/archive/newtype/0.2/doc/html/Control-Newtype.html

instead?

On Thu, 22 Nov 2012 14:15:00 +0000 Vincent Hanquez <tab <at> snarc.org>
wrote:

> 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
(Continue reading)

Vincent Hanquez | 22 Nov 22:14 2012

Re: class Bytestringable or ToBytestring

On 11/22/2012 03:42 PM, kudah wrote:
> Why not use
>
> http://hackage.haskell.org/packages/archive/newtype/0.2/doc/html/Control-Newtype.html
>
> 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
kudah | 23 Nov 05:57 2012
Picon

Re: class Bytestringable or ToBytestring

On Thu, 22 Nov 2012 21:14:31 +0000 Vincent Hanquez <tab <at> snarc.org>
wrote:

> can't find any library on hackage that use it

http://packdeps.haskellers.com/reverse/newtype
Silvio Frischknecht | 23 Nov 09:19 2012
Picon

Re: class Bytestringable or ToBytestring

i recently found the convertible package

http://hackage.haskell.org/packages/archive/convertible/1.0.11.1/doc/html/Data-
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
Vincent Hanquez | 24 Nov 18:42 2012

Re: class Bytestringable or ToBytestring

On 11/23/2012 08:19 AM, Silvio Frischknecht wrote:
> i recently found the convertible package
Thanks. quite a cool package, I'll probably use it in the future for 
some of my programs.

Not sure that's appropriate for my cases unfortunately.

--

-- 
Vincent

Gmane