[Haskell-cafe] Suggestion: "Sizable" super class for Storable

jones.noamle at gmail.com jones.noamle at gmail.com
Thu May 14 08:50:58 UTC 2015

Storable instances have a size, given by sizeOf.

In many cases, we're not interested in peeking/poking data but only passing
it opaquely via the FFI. A common use case is when the C API offers an
"init" function such as:

    void mycontext_init(mycontext *context);

For these cases it would be useful to know the size of "mycontext", so we
could malloc it and pass a pointer to mycontext_init.

Also, it allows Haskell-side code to decide how it wants to allocate the
data, perhaps using some other (external) mechanism not related to the
specific API that the FFI bindings are wrapping.

c2hs would benefit by allowing users to use the '+' notation in function
parameters (which generate  malloc-and-pass style code), without having to
guess the size of the structure. Instead, it could simply use the Sizable
(TM) instance to get the size, and the user will define Sizable in any way
they want (for example, using the {#sizeof#} macro, which is somewhat
unreliable, or by hard-coding or manually entering the size or by some
other method).

Your thoughts are much appreciated!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20150514/505bd571/attachment.html>

More information about the Haskell-Cafe mailing list