Search code examples
chaskellffi

Generate a C struct based on a complex Haskell type


I am trying to use a Haskell library in my C code. The Haskell function I am trying to use has the type String -> IO [Reference] where Reference is a rather complex structure (see here for details).

Based on reading various pieces of documentation it seems that I would have to make this type an instance of Storable and also have a similar struct defined in my c code to be able to access it. This seems like a lot of very repetitive work for such a complex type. Is there a way to automate this? How would one go about doing such a thing?


Solution

  • It depends on you actual use case, but... It could be easier to export Reference as opaque type (via Foreign.StablePtr), and export getter functions to access individual fields.

    Please let me know if you need more details, and I'll expand the answer.