[Haskell-cafe] Practise of hiding contsructors and serialization

Luke Palmer lrpalmer at gmail.com
Sun Feb 22 15:13:45 EST 2009

On Sun, Feb 22, 2009 at 12:55 PM, Marc Weber <marco-oweber at gmx.de> wrote:

> While working on extending scion I got some trouble and wonder which is
> the best way to do this?
> I have to pass a compilation result from a scion process instance to the
> scion daemon process. Of course I'd just like to use a simple Read Show
> for that. However I can't because two datatypes are NominalDiffTime and
> Bag ErrMsg:
> newtype NominalDiffTime = MkNominalDiffTime Pico deriving (Eq,Ord)
> data ErrMsg = ErrMsg {
>        errMsgSpans     :: [SrcSpan],
>        errMsgContext   :: PrintUnqualified,
>        errMsgShortDoc  :: Message,
>        errMsgExtraInfo :: Message
>        }
> Both are fine and both don't export all contstructors so you can't just
> let ghc derive instances for it?
> So the only way is to duplicate those datatypes in order to derive some
> Show, Read instances?
> On the one hand I do understand that you don't want users to access
> constructors directly. On the other hand it would be desirable to derive
> Read, Show instances here.
> Is there a way to get all?
> a) hiding constructors so that you don't have to expose implementation
>  details to the user.
> b) still allow deriving serialization instances?

I usually use Data.Binary for serialization.  There is a cultural standard
not to do anything with Binary instances other than to pass the result of
encode to decode.  They are opaque blobs of bits, perfect for

(I think the only righter way would be to pass the information over a typed
channel that respected privacy)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090222/ab13a0ec/attachment-0001.htm

More information about the Haskell-Cafe mailing list