> A minor point: while not as "spare" as
this, Jonathan's solution does
> not have the problem this one will if there is an
exception in the
> *cleanup* function.
If you wanted a HOF for this, you could factor out my
solution too:
let use_resource init cleanup f x =
init ();
let res = try `Result (f x) with e -> `Exn e in
cleanup ();
match res with
| `Result x -> x
| `Exn e -> raise e;;
To be really neat, init & cleanup could be optional
arguments, in
which you'd need:
let may opt x = match opt with
| None -> ()
| Some f -> f x
(* val may : ('a -> unit) option -> unit *)
and replace init(); with may init (); and similarly for
cleanup --
this way, if you don't need one or the other, then you
don't specify
them in the function call.
http://caml.inria.fr/pub/docs/manual-ocaml/manual006.ht
ml has info on
labels & optional arguments (optionals are a special
case of labels).
Then it's more flexible in that you can use any init &
cleanup
functions you want.
Jonathan
------------------------ Yahoo! Groups Sponsor
--------------------~-->
Check out the new improvements in Yahoo! Groups email.
http://us.click.yahoo.com/6pRQfA/fOaOAA/yQLSAA/saFolB/TM
------------------------------------------------------------
--------~->
Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc.
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http:/
/groups.yahoo.com/group/ocaml_beginners/
<*> To unsubscribe from this group, send an email to:
ocaml_beginners-unsubscribe@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.c
om/info/terms/
|