From: Serge Aleynikov <serge@hq.idt.net>
To: Joel Reymont <joelr1@gmail.com>
Cc: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] Wrapping OCaml function returning a variant
Date: Wed, 28 Mar 2007 08:40:50 -0500 [thread overview]
Message-ID: <460A7062.7050609@hq.idt.net> (raw)
In-Reply-To: <CDE554C6-8389-45AD-A72C-091CD75C88C0@gmail.com>
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
next prev parent reply other threads:[~2007-03-28 12:37 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-03-28 11:33 Joel Reymont
2007-03-28 11:47 ` [Caml-list] " Richard Jones
2007-03-28 12:25 ` micha
2007-03-28 12:51 ` Joel Reymont
2007-03-28 13:28 ` Richard Jones
2007-03-28 14:34 ` micha
2007-03-28 14:46 ` Joel Reymont
2007-03-28 14:56 ` micha
2007-03-28 14:57 ` Xavier Leroy
2007-03-28 15:07 ` Joel Reymont
2007-03-28 22:47 ` Joel Reymont
2007-03-28 12:55 ` Serge Aleynikov
2007-03-28 12:01 ` Joel Reymont
2007-03-28 13:40 ` Serge Aleynikov [this message]
2007-03-28 12:13 ` Joel Reymont
2007-03-28 13:53 ` Serge Aleynikov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=460A7062.7050609@hq.idt.net \
--to=serge@hq.idt.net \
--cc=caml-list@inria.fr \
--cc=joelr1@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox