[ocaml-ctypes] Another dumb question

Jeremy Yallop yallop at gmail.com
Wed Dec 27 13:31:21 GMT 2017

On 27 December 2017 at 05:33, john skaller <skaller at internode.on.net> wrote:
> This should be a quicky….
> Suppose I have some messy struct in C which is generally used
> via a pointer. Eg
>         void f (messy_struct *, int)
> Instead of trying to provide field access I think I’d just like to make it
> abstract. So I would say
>         type messy_t
> and
>         let messy_c = ptr void
> and then
>         let f = foreign “f” = (messy_c @-> int @-> returning void)
> Does that seem right? (where messy_t is the Ocaml type and
> messy_c is the value encoding the view).

The usual way of dealing with this is to define a struct using
'structure' without calling 'field' or 'seal', like this:

   type messy_t
   let messy_struct : messy_t structure typ = structure "messy_struct"
   let messy_c = ptr messy_struct

   let f = foreign “f” = (messy_c @-> int @-> returning void)

Defining a structure without calling 'seal' is like writing an
incomplete type declaration in C, so the code above roughly
corresponds to

   struct messy_struct;
   typedef struct messy_struct *messy_c;

   void f(messy_c, int);

(The 'abstract' type is not often as useful as it seems to be at
first.  The use case I had in mind for it is pthread_t, which can be
different kinds of type -- struct, pointer, integer, etc. -- on
different platforms.  But that sort of thing is pretty rare in

Kind regards,


More information about the Ctypes mailing list