List Info

Thread: "ocaml_beginners"::[] Question on exceptions




"ocaml_beginners"::[] Question on exceptions
user name
2006-06-18 23:46:37
Jon Harrop wrote:
> On Sunday 18 June 2006 13:24, Till Crueger wrote:
>> let apply func =
>>     init ();
>>     let retVal =
>>        begin
>>           try
>>              func ();
>>           with a ->
>>              cleanup ();
>>              raise a
>>         end
>>     cleanup ();
>>     retVal
>>
>> but here I have th cleanup step twice. Is there a
simple way to eliminate
>> one of the cleanups and still have the same
behaviour?
> 
> Yes. You just factor this functionality out into a
higher-order function that 
> accepts the cleanup function as an argument. This
higher-order function is 
> typically called "unwind protect":
> 
> let unwind_protect body cleanup =
>   try body(); cleanup() with e -> cleanup(); raise e

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.

Robert Roessler
robertrrftp.com
http://www.rftp.com


------------------------ Yahoo! Groups Sponsor
--------------------~--> 
Yahoo! Groups gets a make over. See the new email design.
http://us.click.yahoo.com/XISQkA/lOaOAA/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/
 


"ocaml_beginners"::[] Question on exceptions
user name
2006-06-19 02:41:36
> 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/
 


"ocaml_beginners"::[] Question on exceptions
user name
2006-06-19 01:31:55
On Monday 19 June 2006 00:46, Robert Roessler wrote:
> > let unwind_protect body cleanup =
> >   try body(); cleanup() with e -> cleanup();
raise e
>
> 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.

Will this preserve those semantics:

let unwind_protect body cleanup =
  (try body() with e -> cleanup(); raise e); cleanup()

Of course, you're in problems anyway if your cleanup
function is also raising 
exceptions. Do you really want to ignore the exception from
"body"?

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scient
ists


------------------------ Yahoo! Groups Sponsor
--------------------~--> 
Yahoo! Groups gets a make over. See the new email design.
http://us.click.yahoo.com/XISQkA/lOaOAA/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/
 


[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )