caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Conglun Yao" <yaoconglun@gmail.com>
To: caml-list@yquem.inria.fr
Subject: How to handle try .... finally properly?
Date: Wed, 10 Dec 2008 11:15:43 +0000	[thread overview]
Message-ID: <f7b50d2a0812100315m6c153153y59afc86e02ae8a1e@mail.gmail.com> (raw)

Sorry, no replies in ocaml-beginner, post it again in caml-list.


Dear all,

I'm thinking about how to handle try .... finally in ocaml, and found
one of camlp4 implementations from
http://bluestorm.info/camlp4/dev/try/pa_tryfinally.ml.html

Example code adopted from Gabriel Scherer's pa_tryfinally

   Example Input :

     let transform f x =
       GlMat.push();
       try f x
       finally GlMat.pop()

   Output :

   let transform f x =
     (GlMat.push ();
     let after () = GlMat.pop ()
     and result = try `Result (f x) with | exn -> `Exn exn
     in (after ();
         match result with
         | `Result v -> v
         | `Exn e -> raise e))

At the first glance, it answered my question. However, it solved the
problem partially, only working on functions with one argument.

If we feed the * transform * a function with more than one argument,
(it is possible because of curring)

transform (fun x y -> .... some logic staff .... ) x y

will invoke the * after () * before the ((fun x y -> .....) x) y is
really executed.

Ideally, * transform * function is specified as allowing a function f
with only one parameter. But it seems impossible in OCaml,
as f : 'a -> 'b doesn't means f can and only accept one argument. ( 'b
could be 'c -> 'd ) If we could constraint 'b as a non-function type,
it can solve the problem.

Or someone knows a better solution to work around the problem.

Thanks.

Conglun


             reply	other threads:[~2008-12-10 11:15 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-12-10 11:15 Conglun Yao [this message]
2008-12-10 22:58 ` [Caml-list] " blue storm
2008-12-11  0:09 ` Jacques GARRIGUE
2008-12-11  0:52   ` Conglun Yao

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=f7b50d2a0812100315m6c153153y59afc86e02ae8a1e@mail.gmail.com \
    --to=yaoconglun@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    /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).