Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] beginner question about camlp4
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-01-23 (10:16)
From: Daniel de Rauglaudre <daniel.de_rauglaudre@i...>
Subject: Re: [Caml-list] beginner question about camlp4

On Tue, Jan 22, 2002 at 08:45:10PM -0600, Doug Bagley wrote:

>   let chan = open_in "file" in
>   try
>     begin process chan end;
>     close chan
>   with
>     Ouch -> close chan; maybe_do_something
>   | e -> close chan; raise e;;

No problem about your Camlp4 code, and it is indeed a possible usage
of Camlp4. But I would have written a different construction, because
I consider than your first "close chan" should be outside the try and
I would like to be able to return something (get the result of your
"process chan").

My construction would be something like this:
     some_expression always some_cleanup

which would be converted into:
     let x = try some_expression with e -> some_cleanup; raise e in

Well, but perhaps an infix construction is not very readable here.

> let safely setup cleanup subject f =
>   let x = setup subject in
>   try f x; cleanup x with e -> cleanup x; raise e

For the same reason, I would prefer:

 let safely setup cleanup subject f =
   let x = setup subject in
   let r = try f x with e -> cleanup x; raise e in
   cleanup x;

This way, "safely" could return the value of "f x" when it is ok.

Now, Camlp4 or function "safely", it is a matter of personal opinion.

Bug reports:  FAQ:
To unsubscribe, mail  Archives: