[Haskell-cafe] Re: Data.Binary Endianness

apfelmus apfelmus at quantentunnel.de
Mon Sep 10 15:02:09 EDT 2007


Sven Panne wrote:
 > So what I was asking for is:
> 
>    getInt32be, putIEEEFloatLe, getIEEEDoubleHost, ...
> 
> Type classes might be used to get a slightly smaller API, but I am unsure 
> about the performance impact and how much this would really buy us in terms 
> of the ease of use of the API.

It's not that related, but I just got struck by an obvious idea, namely 
to put the endianness in an extra parameter

   data Endianness = Little | Big | Host
   putInt32 :: Endianness -> Int -> Put

in order to avoid three functions for every data type. I don't know 
whether this makes performance a bit worse, but if it does, phantom 
types or similar are an option to encode endianness, too.

   data LE = LE
   data BE = BE
   data Host = Host

   class Put a endian where
     put :: endian -> a -> Put

   instance Put Int32 LE where ...
   instance Put Int32 BE where ...
   instance Put Int32 Host where ...

Oh, and the 8,16,32 and 64 are good candidates for phantom 
type/associated data types, too.

Regards,
apfelmus



More information about the Haskell-Cafe mailing list