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

Martin Huschenbett huschi at gmx.org
Sun Mar 1 12:42:34 EST 2009


you could do something like

 > instance (Show a,Read a) => Binary a where
 >  put = put . show
 >  get = fmap read get

But then you will need the following language extensions: 
FlexibleInstances, OverlappingInstances, UndecidableInstances

I don't know how safe this is but it seems to work.



Svein Ove Aas schrieb:
> 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..
> Well, I understand why this would be somewhere between extremely hard
> and impossible in haskell '98, but I'm not entirely up on all the
> extensions, so I thought I'd ask - given every extension implemented
> in ghc 6.10.1, is there any reasonable way to do this?
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

More information about the Haskell-Cafe mailing list