From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 66C25BC69 for ; Wed, 28 Mar 2007 13:52:14 +0200 (CEST) Received: from mta2.srv.hcvlny.cv.net (mta2.srv.hcvlny.cv.net [167.206.4.197]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l2SBqDCX023042 for ; Wed, 28 Mar 2007 13:52:14 +0200 Received: from [192.168.15.101] (ool-457da870.dyn.optonline.net [69.125.168.112]) by mta2.srv.hcvlny.cv.net (Sun Java System Messaging Server 6.2-6.01 (built Apr 3 2006)) with ESMTP id <0JFM00MAQ3N069E0@mta2.srv.hcvlny.cv.net> for caml-list@inria.fr; Wed, 28 Mar 2007 07:52:13 -0400 (EDT) Date: Wed, 28 Mar 2007 07:55:07 -0500 From: Serge Aleynikov Subject: Re: [Caml-list] Wrapping OCaml function returning a variant In-reply-to: To: Joel Reymont Cc: Caml List Message-id: <460A65AB.8020902@hq.idt.net> MIME-version: 1.0 Content-type: text/plain; charset=ISO-8859-1; format=flowed Content-transfer-encoding: 7BIT References: User-Agent: Thunderbird 1.5.0.10 (Windows/20070221) X-Miltered: at concorde with ID 460A56ED.002 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocaml:01 ocaml:01 c-interface:01 alloc:01 ocaml's:01 ocaml's:01 heap:01 heap:01 sourceforge:01 garbage:01 wrote:01 caml-list:01 functions:01 int:01 int:01 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 http://caml.inria.fr/pub/docs/manual-ocaml/manual032.html Then if still unclear, examine: http://oracaml.sourceforge.net/ or any other C-interface library available on Caml Hump: http://caml.inria.fr/cgi-bin/hump.en.cgi?sort=0&browse=65 > 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. Serge