Version française
Home     About     Download     Resources     Contact us    
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: micha <micha-1@f...>
Subject: Re: [Caml-list] Wrapping OCaml function returning a variant
Am Wed, 28 Mar 2007 12:33:46 +0100
schrieb Joel Reymont <joelr1@gmail.com>:

> 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
> 

the labels are numbered starting from zero, so in c have have to
decode two possible values :

let a= Success("xxx") ...

is in C a block with tag 0 and size 1, to get the string write:

char* s = String_val(Field(a,0))

for the case

let b = Error("e",1,2,3) ...

you have a block with tag 1 and size 4:
to get the values you write:
char* err=String_val(Field(b,0));
int n1= Int_val(Field(b,1));
...
int n3= Int_val(Field(b,3));


hope that helps...

 Michael


ps: polymorphic variants are handled different...