caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] C binding with labelled arguments
@ 2012-11-07 15:54 john.else
  2012-11-07 16:16 ` Gabriel Scherer
  0 siblings, 1 reply; 5+ messages in thread
From: john.else @ 2012-11-07 15:54 UTC (permalink / raw)
  To: caml-list

Hi,

If I have a C function like so:

CAMLprim value stub_add_ints(value first, value second) {
  CAMLparam2(first, second);
  int sum;

  sum = Int_val(first) + Int_val(second);
  CAMLreturn(Val_int(sum));
}

and I write an OCaml binding for it:

external add_ints : int -> int -> int  = "stub_add_ints"

is there any way to give the OCaml function labelled arguments, other than
writing a pure OCaml wrapper function? I'd like to be able to call

add_ints ~first:3 ~second:4;;

instead of just

add_ints 3 4;;

If I define the binding like this:

external add_ints : first:int -> second:int -> int  = "stub_add_ints"

it compiles, but the labels seem to be ignored - calling the function with
labelled arguments gives the error "This argument cannot be applied with label
~first", and looking up the function's type with OCamlSpotter indeed shows
that the function's type has no label information.

Thanks,
John

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] C binding with labelled arguments
  2012-11-07 15:54 [Caml-list] C binding with labelled arguments john.else
@ 2012-11-07 16:16 ` Gabriel Scherer
  2012-11-07 16:26   ` john.else
  0 siblings, 1 reply; 5+ messages in thread
From: Gabriel Scherer @ 2012-11-07 16:16 UTC (permalink / raw)
  To: john.else; +Cc: caml-list

Given that the argument passing order of labelled functions is not
specified, it seems like it would be really safer to use a wrapper
function. Why wouldn't you do this?
(If that is for performance reason, the wrapper should be inlined when
used from inside the same module, and if you want to expose the
external globally you can always re-define it along with its wrapper
in any module.)

On Wed, Nov 7, 2012 at 4:54 PM,  <john.else@citrix.com> wrote:
> Hi,
>
> If I have a C function like so:
>
> CAMLprim value stub_add_ints(value first, value second) {
>   CAMLparam2(first, second);
>   int sum;
>
>   sum = Int_val(first) + Int_val(second);
>   CAMLreturn(Val_int(sum));
> }
>
> and I write an OCaml binding for it:
>
> external add_ints : int -> int -> int  = "stub_add_ints"
>
> is there any way to give the OCaml function labelled arguments, other than
> writing a pure OCaml wrapper function? I'd like to be able to call
>
> add_ints ~first:3 ~second:4;;
>
> instead of just
>
> add_ints 3 4;;
>
> If I define the binding like this:
>
> external add_ints : first:int -> second:int -> int  = "stub_add_ints"
>
> it compiles, but the labels seem to be ignored - calling the function with
> labelled arguments gives the error "This argument cannot be applied with label
> ~first", and looking up the function's type with OCamlSpotter indeed shows
> that the function's type has no label information.
>
> Thanks,
> John
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Re: [Caml-list] C binding with labelled arguments
  2012-11-07 16:16 ` Gabriel Scherer
@ 2012-11-07 16:26   ` john.else
  2012-11-07 16:40     ` Florent Monnier
  0 siblings, 1 reply; 5+ messages in thread
From: john.else @ 2012-11-07 16:26 UTC (permalink / raw)
  To: caml-list, gabriel.scherer

Hi Gabriel,

I agree it'd be better to use labelled arguments than not, for safety as you
suggest. I was just wondering if there was a way to add labelled arguments
directly to the external, rather than requiring a wrapper function to add the
labels.

John

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Re: [Caml-list] C binding with labelled arguments
  2012-11-07 16:26   ` john.else
@ 2012-11-07 16:40     ` Florent Monnier
  2012-11-07 17:02       ` John Else
  0 siblings, 1 reply; 5+ messages in thread
From: Florent Monnier @ 2012-11-07 16:40 UTC (permalink / raw)
  To: john.else; +Cc: caml-list, gabriel.scherer

Hi,
I cannot reproduce your error, for me it compiles fine:

$ cat foo_stub.c foo.ml
#include <caml/mlvalues.h>
#include <caml/memory.h>

CAMLprim value stub_add_ints(value first, value second) {
  CAMLparam2(first, second);
  int sum;
  sum = Int_val(first) + Int_val(second);
  CAMLreturn(Val_int(sum));
}

external add_ints : first:int -> second:int -> int  = "stub_add_ints"

let () =
  let res = add_ints ~first:3 ~second:4 in
  print_int res;
  print_newline ()
$ ocamlopt -o foo.opt foo.ml foo_stub.c
$ ./foo.opt
7

-- 

2012/11/7, john.else@citrix.com <john.else@citrix.com>:
> Hi Gabriel,
>
> I agree it'd be better to use labelled arguments than not, for safety as
> you
> suggest. I was just wondering if there was a way to add labelled arguments
> directly to the external, rather than requiring a wrapper function to add
> the
> labels.
>
> John
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: Re: [Caml-list] C binding with labelled arguments
  2012-11-07 16:40     ` Florent Monnier
@ 2012-11-07 17:02       ` John Else
  0 siblings, 0 replies; 5+ messages in thread
From: John Else @ 2012-11-07 17:02 UTC (permalink / raw)
  To: Florent Monnier; +Cc: caml-list, gabriel.scherer

Hi Florent,

That also works for me - not sure what I was doing wrong before.
I'll go back and experiment on my "real" code. Thanks all!

John

-----Original Message-----
From: Florent Monnier [mailto:monnier.florent@gmail.com] 
Sent: 07 November 2012 16:41
To: John Else
Cc: caml-list@inria.fr; gabriel.scherer@gmail.com
Subject: Re: Re: [Caml-list] C binding with labelled arguments

Hi,
I cannot reproduce your error, for me it compiles fine:

$ cat foo_stub.c foo.ml
#include <caml/mlvalues.h>
#include <caml/memory.h>

CAMLprim value stub_add_ints(value first, value second) {
  CAMLparam2(first, second);
  int sum;
  sum = Int_val(first) + Int_val(second);
  CAMLreturn(Val_int(sum));
}

external add_ints : first:int -> second:int -> int  = "stub_add_ints"

let () =
  let res = add_ints ~first:3 ~second:4 in
  print_int res;
  print_newline ()
$ ocamlopt -o foo.opt foo.ml foo_stub.c
$ ./foo.opt
7

-- 

2012/11/7, john.else@citrix.com <john.else@citrix.com>:
> Hi Gabriel,
>
> I agree it'd be better to use labelled arguments than not, for safety 
> as you suggest. I was just wondering if there was a way to add 
> labelled arguments directly to the external, rather than requiring a 
> wrapper function to add the labels.
>
> John
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2012-11-07 17:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-07 15:54 [Caml-list] C binding with labelled arguments john.else
2012-11-07 16:16 ` Gabriel Scherer
2012-11-07 16:26   ` john.else
2012-11-07 16:40     ` Florent Monnier
2012-11-07 17:02       ` John Else

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).