Browse thread
How to handle try .... finally properly?
- Conglun Yao
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Conglun Yao <yaoconglun@g...> |
| Subject: | How to handle try .... finally properly? |
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