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=1.4 required=5.0 tests=SPF_NEUTRAL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 42EA7BC0B for ; Wed, 17 Jan 2007 13:53:49 +0100 (CET) Received: from wr-out-0506.google.com (wr-out-0506.google.com [64.233.184.230]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l0HCrm7L003325 for ; Wed, 17 Jan 2007 13:53:48 +0100 Received: by wr-out-0506.google.com with SMTP id 69so1687134wra for ; Wed, 17 Jan 2007 04:53:48 -0800 (PST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:sender:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references:x-google-sender-auth; b=tPwh9tnJbvxsLwUM8UQak5ovxpX4FryS0mZ54hUYoi/hDrcOg3pWJenip3xECvXSLyev2FjunfPw8/ZNSgkyAkkSwgT8kio5eIgCHIxssyaR8KqoadWTbYfF5eVIL55RIL9OaeRIl1NIei/Cu7MEcF6kPB+fclYVtZTlzyY5AWg= Received: by 10.90.78.9 with SMTP id a9mr6023320agb.1169038427805; Wed, 17 Jan 2007 04:53:47 -0800 (PST) Received: by 10.90.72.8 with HTTP; Wed, 17 Jan 2007 04:53:47 -0800 (PST) Message-ID: <95513600701170453i28e0fb2scefc39d4774ddaca@mail.gmail.com> Date: Wed, 17 Jan 2007 13:53:47 +0100 From: "Olivier Andrieu" Sender: oandrieu@gmail.com To: "Vincent Hanquez" Subject: Re: [Caml-list] Ocaml compiler features Cc: caml-list@yquem.inria.fr In-Reply-To: <20070117114104.GA28532@snarc.org> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline 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> <20070117114104.GA28532@snarc.org> X-Google-Sender-Auth: 1a930e8ab30a8650 X-Miltered: at discorde with ID 45AE1C5C.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; andrieu:01 oandrieu:01 ocaml:01 compiler:01 ocaml:01 trivial:01 exn:01 exn:01 higher-order:01 high-level:01 mult:01 wrote:01 wrote:01 syntactic:01 exception:01 On 1/17/07, Vincent Hanquez wrote: > 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 that's not clear-cut ; it depends on the cleanup function. For instance, concerning I/O channels in the standard library, you have close_out and close_out_noerr. They do not have the same behavior, and you should rather use the _no_err one in the with exn part. > 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. Just use a higher-order function: it won't duplicate code, it's high-level and it doesn't require any syntactic sugar. let with_file_out fname f = let oc = open_out fname in try let r = f oc in close_out oc ; r with exn -> close_out_noerr oc ; raise exn let with_mat m f = GlMat.push () ; let r = try GlMat.mult m ; f () with exn -> GlMat.pop () ; raise exn in GlMat.pop () ; r