[Haskell-beginners] Data and Typeable for an opaque type

Ertugrul Söylemez ertesx at gmx.de
Tue Apr 28 14:29:42 UTC 2015


> data Font_Opaque
> type Font = Ptr Font_Opaque
>
> [...]
>
> Anyway, the rough-and-ready approach for this would be to manually
> define instances of Data and Typeable for the opaque type - my
> question is: what would sensible implementations look like for such a
> type?

Manual instances of those are usually highly questionable.  While you
probably won't blow up anything with a manual Data instance, a manual
Typeable instance is in the critical danger zone alongside unsafeCoerce.

First of all Ptr is already Typeable and for concrete types like
Font_Opaque you can derive Typeable.  That makes Font Typeable.

If you need a Data instance for processing, then one way to get it is to
build an isomorphism between Font and a regular Haskell type.  Start by
rewriting Font to be a newtype wrapper.  Derive Data for the helper type
and write the Data instance for Font in terms of it.  This might save
you a lot of work, or it may create even more work depending on the API
you're depending on.

Final alternative:  You do what many of us have done many times.  In
some cases implementing a Haskell variant of a foreign library from
scratch is more efficient than trying to deal with a clumsy C API.
Often this is not an option, but when it is, it's usually a good one.


Greets,
Ertugrul
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 472 bytes
Desc: not available
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150428/a42ed748/attachment.sig>


More information about the Beginners mailing list