[Haskell-cafe] Supplying a default implementation for a typeclass based on another class

Sterling Clover s.clover at gmail.com
Sat Feb 28 23:18:18 EST 2009

On Feb 28, 2009, at 5:59 AM, Svein Ove Aas wrote:

> I'm in the process of writing a distributed filesystem (in haskell,
> yay), which of course means I'm using Binary for de/serialization.
> Now, that's fine enough, but for simplicity (and for wireshark), I'd
> like to be able to have Binary fall back on an instance based on
> Show/Read for any type that lacks any other Binary instance.

Rather than something funny with extensions, why not write two  
methods, of type

defBinaryPut :: Show a => a -> Put
defBinaryGet :: Read a => Get a

Then, if somebody tries to use something lacking a Binary instance,  
they can just define a simple instance with those two methods. And  
when they want to improve performance, they just write better  
methods. :-)

In fact, these seem useful enough to go into, if not Data.Binary  
itself, a separate package. After all, proper serialization shouldn't  
be a barrier to rapid prototyping.


More information about the Haskell-Cafe mailing list