ANN: H98 FFI Addendum 1.0, Release Candidate 10

Simon Marlow simonmar at microsoft.com
Mon Jun 2 10:25:24 EDT 2003


 
> Using `newtype T = MkT Int8' or equivalent only works for foreign data
> types whose representation is known.  How do you deal with 
> C's "time_t",
> for example?  Or with C "struct" types?  The user doesn't want to make
> non-portable assumptions about what padding the C compiler is going
> to insert.
> 
> Like Haskell, Mercury allows data definitions with no body:
> 
> 	:- type t.
> 
> But to deal with cases like the ones mentioned above,
> Mercury also allows the user to declare how such types are
> represented in foreign languages, using `pragma foreign_type'
> declarations:
> 
> 	:- pragma foreign_type("C", t, "some_c_typename").
> 
> The Mercury compiler will generate appropriate glue code to 
> marshal and unmarshal values of that type.

We have similar support in some of the Haskell FFI Tools.  For example,
using hsc2hs I can write:

newtype CTime = CTime $(type time_t)

and this will be converted into

newtype CTime = CTime (Int32)

(or whatever) when hsc2hs processes the source.  Ok, it's
language-dependent, but it has the advantage that it doesn't require any
extra support from the compiler, and nothing needs to be added to the
FFI spec.

Similar mechanisms can be (and are) used for marshalling C structs
without baking in the field offsets and types.

Cheers,
	Simon




More information about the FFI mailing list