I have some questions about the ffi in haskell
.
first of all i'm trying to work with c structs in haskell
.
there i have some questions: i have a struct like
struct foo{int a; float b;};
data Foo = Foo { a :: Int, b :: Float } deriving (Show, Eq)
okay now a question about FunPtr
FunPtr
why a normal definition like Ptr CInt -> IO CInt
is not enough?Marshalling
To marshal structures, you will need to use a Storable
class instance to marshal data back and forth, via peek
and poke
.
See this previous answer for an example: How to use hsc2hs to bind to constants, functions and data structures?
FunPtr
FunPtr
is only needed when you want to pass a function across the FFI boundary as a first-class value, not for calling foreign functions. Precisely:
A value of type FunPtr a is a pointer to a function callable from foreign code. The type a will normally be a foreign type, a function type with zero or more arguments
An example, registering a call back function:
foreign import ccall "stdlib.h &free"
p_free :: FunPtr (Ptr a -> IO ())
Since we have to pass p_free
itself to a Haskell function, we have to let Haskell know this is actually a C function. The FunPtr
wrapper controls that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With