From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 6AEBBBC0B for ; Wed, 17 Jan 2007 12:42:00 +0100 (CET) Received: from hades.snarc.org (hades.snarc.org [212.85.152.11]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l0HBfxlP002700 for ; Wed, 17 Jan 2007 12:42:00 +0100 Received: by hades.snarc.org (Postfix, from userid 1000) id B70E81B481; Wed, 17 Jan 2007 12:41:04 +0100 (CET) Date: Wed, 17 Jan 2007 12:41:04 +0100 To: skaller Cc: Edgar Friendly , caml-list@yquem.inria.fr Subject: Re: [Caml-list] Ocaml compiler features Message-ID: <20070117114104.GA28532@snarc.org> References: <20070115000544.GA28731@snarc.org> <20070115221717.GA9982@snarc.org> <1168910291.9207.62.camel@rosella.wigram> <20070116090002.GA14494@snarc.org> <1168956872.5332.28.camel@rosella.wigram> <20070116150027.GA17519@snarc.org> <1168969667.5178.18.camel@rosella.wigram> <45AD2672.2090206@gmail.com> <1169004527.8941.77.camel@rosella.wigram> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1169004527.8941.77.camel@rosella.wigram> X-Warning: Email may contain unsmilyfied humor and/or satire. User-Agent: Mutt/1.5.13 (2006-08-11) From: tab@snarc.org (Vincent Hanquez) X-Miltered: at concorde with ID 45AE0B87.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocaml:01 compiler:01 ocaml:01 trivial:01 exn:01 exn:01 variants:01 hashtbl:01 wrote:01 exception:01 exception:01 caml-list:01 inline:01 exceptions:01 argument:02 On Wed, Jan 17, 2007 at 02:28:47PM +1100, skaller wrote: > That is a reasonable argument to examine: but I'm not sure > finally is actually all that useful. If you're thinking of: > > try maybe_throw X; do something > finally -> cleanup done > > then it can be coded in Ocaml like: > > begin try maybe_throw X; do something; cleanup > with x -> cleanup; throw x end It would be more convincing if you hadn't made the trivial mistake to put the first cleanup function in the try. if for a really weird reason cleanup raises an exception, cleanup is done twice. the correct OCaml code is: let r = try maybe_throw X; do something; with exn -> cleanup; raise exn in cleanup; r the point is not it's hard to do, it's annoything to duplicate here and there, and doing the inline code lose the higher level view we can have with a syntaxic sugar like try-finally. > try > before > open f > middle > close f > after > with _ -> close f > > then you have all sorts of problems if a different exception > is thrown in the before or after parts. Using type information > to distinguish the kind of exception is clearly a hack: > what if you're opening two files? The thing is you don't do this with a flat block. If you need to unwind a complex state you do: before try let fd = open fd try middle finally close fd finally after > Yes, but you can do what many people do: map the exceptions into > variants like: > > let hfind x y = > try Some(Hashtbl.find x y) > with Not_found -> None I'm not arguing against this method. You do it the way you want, but when you use exception, having a finally is very handy for a lots of case. Nothing more, nothing less. > let fopen f lock = try Some (open f) with Error -> None in > let lck = mutex () in lock mutex; > let f = fopen name lck in > match f with > | Some file -> ... close, release lck > | None -> release lck > > WOOPS! Again we had to repeat the cleanup code. > Releasing the mutex AFTER the whole code works, but > if we want separate control flows for the two cases > we have to test the variant again: > > begin match f with .. end; release lck; > match f with .. (* fork again *) > > So actually, you could use a 'finally' here too: seems that you *finally* understand it ;) -- Vincent Hanquez