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 563C2BC69 for ; Wed, 28 Mar 2007 14:37:57 +0200 (CEST) Received: from mta5.srv.hcvlny.cv.net (mta5.srv.hcvlny.cv.net [167.206.4.200]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l2SCbu6P000951 for ; Wed, 28 Mar 2007 14:37:57 +0200 Received: from [192.168.15.101] (ool-457da870.dyn.optonline.net [69.125.168.112]) by mta5.srv.hcvlny.cv.net (Sun Java System Messaging Server 6.2-6.01 (built Apr 3 2006)) with ESMTP id <0JFM00FWA5R7N5C0@mta5.srv.hcvlny.cv.net> for caml-list@inria.fr; Wed, 28 Mar 2007 08:37:56 -0400 (EDT) Date: Wed, 28 Mar 2007 08:40:50 -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: <460A7062.7050609@hq.idt.net> MIME-version: 1.0 Content-type: text/plain; charset=ISO-8859-1; format=flowed Content-transfer-encoding: 7BIT References: <460A65AB.8020902@hq.idt.net> User-Agent: Thunderbird 1.5.0.10 (Windows/20070221) X-Miltered: at concorde with ID 460A61A4.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocaml:01 c-interface:01 variants:01 syntax:01 foo:01 syntax:01 variants:01 constructors:01 ocaml:01 toplevel:01 camlp:01 foo:01 val:01 constructors:01 val:01 Joel Reymont wrote: > > On Mar 28, 2007, at 1:55 PM, Serge Aleynikov wrote: > >> You can read the section 18.3.4 of >> http://caml.inria.fr/pub/docs/manual-ocaml/manual032.html > > It does not clearly explain how to handle variant types in C code. Maybe > I'm dumb but I did not understand it. > >> 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 > > It looks like you are using polymorphic variants in new syntax. Maybe > I'm wrong but it doesn't work for me > > # type foo = [Null | Int of int];; > Syntax error That's because this code is written using revised (http://caml.inria.fr/pub/docs/tutorial-camlp4/tutorial005.html) syntax. These are not polymorphic variants but rather a basic union type with non-constant constructors. Hint: you can start the ocaml toplevel like this to get it understand revised syntax: "ocaml camlp4r.cma". > I still don't understand how I can handle regular variants of the > following type returned by OCaml to the C side In terms of your example above, on the C side you would do: void bar(value foo) { if (Is_long(foo)) { switch (Long_val(foo)) { case 0: /* Null */ ... break; /* if there are other constant constructors besides Null add additional cases here */ default: fail_exception(Long_val(foo), "Unknown constant constructor!"); } } else { switch (Tag_val(foo)) { case 0: /* Int of int */ /* Access the non-constant value by Long_val(Field(foo,0))) */ break; ... } } } Regards, Serge