[Haskell-cafe] Reducing boilerplate
Sylvain Henry
hsyl20 at gmail.com
Sat Mar 5 13:56:17 UTC 2016
Hi,
To write FFI bindings, I use c-storable-deriving [1] to automatically
derive CStorable instances for many data types (the only difference
between Storable and CStorable is that CStorable provides default
methods for types that have Generic instances):
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}
...
data X = X
{ fieldStatus :: Vector 24 Word8
, fieldPadding :: Word8
} deriving (Generic, CStorable)
However I also need a Storable instance, hence I have to write (the
"c*" methods are provided by CStorable):
instance Storable X where
peek = cPeek
poke = cPoke
alignment = cAlignment
sizeOf = cSizeOf
Is there a way to automatically generate this instance for every data
that has an instance of CStorable? Ideally, I would like to say once
and for all:
instance CStorable a => Storable a where
peek = cPeek
poke = cPoke
alignment = cAlignment
sizeOf = cSizeOf
As I don't think it is currently possible, would it be sensible to add
the support for automatically derived instances attached to classes
(as a GHC extension)?
Regards,
Sylvain
[1] https://hackage.haskell.org/package/c-storable-deriving
More information about the Haskell-Cafe
mailing list