caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* How to handle try .... finally properly?
@ 2008-12-10 11:15 Conglun Yao
  2008-12-10 22:58 ` [Caml-list] " blue storm
  2008-12-11  0:09 ` Jacques GARRIGUE
  0 siblings, 2 replies; 4+ messages in thread
From: Conglun Yao @ 2008-12-10 11:15 UTC (permalink / raw)
  To: caml-list

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


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

end of thread, other threads:[~2008-12-11  0:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-12-10 11:15 How to handle try .... finally properly? Conglun Yao
2008-12-10 22:58 ` [Caml-list] " blue storm
2008-12-11  0:09 ` Jacques GARRIGUE
2008-12-11  0:52   ` Conglun Yao

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