caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Marek Kubica <marek@xivilization.net>
To: Gabriel Scherer <gabriel.scherer@gmail.com>
Cc: "Török Edwin" <edwin+ml-ocaml@etorok.net>, caml-list@inria.fr
Subject: Re: [Caml-list] C interop: Return values in parameters
Date: Mon, 31 Dec 2012 02:48:08 +0100	[thread overview]
Message-ID: <20121231024808.6ed61bdc@xivilization.net> (raw)
In-Reply-To: <CAPFanBFe6bgnhjuVf01+O0Y0BH+Z0pVd=UFZQt+GLiRD-87t1A@mail.gmail.com>

Hello Gabriel,

Thanks for your mail. I read it multiple times to grasp all information.

On Sun, 30 Dec 2012 15:36:16 +0100
Gabriel Scherer <gabriel.scherer@gmail.com> wrote:

> References are a derived concept defined as:
> 
>   type 'a ref = { mutable contents : 'a }
> 
> You can update them from the C side just as you would handle a
> polymorphic record, with Field and Store_field.

Turns out Field is also valid as L-Value so I can even use Field(x,0) to
set. Fun :) With this solution, I managed to fix my immediate problem.

> In the code you show, the OCaml value corresponding to the pointer is
> exactly the pointer, hidden as a 'value' type. This is correct as
> OCaml detects out-of-(OCaml)-heap pointer. However, if you used a
> custom block instead (
> http://caml.inria.fr/pub/docs/manual-ocaml-4.00/manual033.html#toc150
> ), OCaml would do the boxing for you: Data_custom_val(v) already
> returns a pointer than can be dereferenced or mutated.

I read this as well as
<http://www.linux-nantes.org/~fmonnier/ocaml/ocaml-wrapping-c.php#ref_custom>
and while I seem to understand how it works, I don't get how to modify
it.

Do I understand right that I'd just create such a custom block and just
wrap the pointer into it? I'll probably implement it tomorrow and see
how I like it.

> I think you have a choice between using references explicitly for
> those functions of the API that mutate input references, or uniformly
> representing this type of data as a custom block. The latter option
> may be valuable if you have uses for the other features of custom
> blocks, eg. the user-defined comparison and finalization operations,
> and probably not worth the trouble otherwise. Finally, explicitly
> using references to signal mutability in some part of your API is
> probably clearer and a better design.

Well, except for finalization I don't really have much use for the
operations that custom blocks expose, but the finalization might be
worth the work.

Thanks again, I really felt productive today, by getting things done.

regards,
Marek

      reply	other threads:[~2012-12-31  1:48 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-30 13:08 Marek Kubica
2012-12-30 14:01 ` Török Edwin
2012-12-30 14:19   ` Marek Kubica
2012-12-30 14:36     ` Gabriel Scherer
2012-12-31  1:48       ` Marek Kubica [this message]

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=20121231024808.6ed61bdc@xivilization.net \
    --to=marek@xivilization.net \
    --cc=caml-list@inria.fr \
    --cc=edwin+ml-ocaml@etorok.net \
    --cc=gabriel.scherer@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;
as well as URLs for NNTP newsgroup(s).