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: Joel Reymont <joelr1@g...>
Subject: Re: [Caml-list] Wrapping OCaml function returning a variant
I came up with this. Is it sound?

char *morph(char *code, char** error, int *line, int *start, int *end)
{
   static value *closure = NULL;
   value v;
   char *res;

   if (closure == NULL)
     closure = caml_named_value("morph");

   v = callback(*closure, caml_copy_string(code));

   switch (Long_val(v)) {
   case 0: /* success */
     res = String_val(Field(v, 0));
     *error = NULL;
   case 1: /* error  */
     res = NULL;
     *error = String_val(Field(v, 0));
     *line = Int_val(Field(v, 1));
     *start = Int_val(Field(v, 2));
     *end = Int_val(Field(v, 3));
   }

   return res;
}

	Thanks, Joel

On Mar 28, 2007, at 3:34 PM, micha wrote:

> that may look like:
>
> CAMLprim value cfunc(value x)
> {
> CAMLparam1(x);
> switch (Tag_val(x)) {
> 	case 0: do something with first variant;
> 	case 1: do something  with second variant
> 	default: argh;
> }
> CAMLreturn(Val_unit);
> }

--
http://wagerlabs.com/