Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Wrapping OCaml function returning a variant
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Serge Aleynikov <serge@h...>
Subject: Re: [Caml-list] Wrapping OCaml function returning a variant
Joel Reymont wrote:
> Is there an example of returning a variant from OCaml into C somewhere?
> It's a regular variant, declared like this
> type morpher_output =
>   | Success of string
>   | Error of string * int * int * int

You can read the section 18.3.4 of

Then if still unclear, examine:
or any other C-interface library available on Caml Hump:

> The other issue is that I don't know how to deal with strings allocated 
> in OCaml. Will they be  collected automatically?

If you are using caml_copy_string() or caml_alloc_string() they allocate 
strings on OCaml's heap, and will be automatically garbage collected.

> The strings _must_ be allocated in OCaml but I can guarantee that they 
> will not be modified in the C code. Any suggestions on how to deal with 
> this?

Unless I am not understanding your question, the above two functions are 
the way to allocate strings on OCaml's heap.