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.1 required=5.0 tests=AWL,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 mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id EF898BC6C for ; Mon, 28 Jan 2008 17:21:20 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAMmPnUfAXQImh2dsb2JhbACQKgEBAQgKKZpl X-IronPort-AV: E=Sophos;i="4.25,260,1199660400"; d="scan'208";a="6663560" Received: from discorde.inria.fr ([192.93.2.38]) by mail2-smtp-roc.national.inria.fr with ESMTP; 28 Jan 2008 17:21:20 +0100 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id m0SGLKbA030378 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Mon, 28 Jan 2008 17:21:20 +0100 X-IronPort-AV: E=Sophos;i="4.25,260,1199660400"; d="scan'208";a="7337348" Received: from peray.inria.fr (HELO ausone.inria.fr) ([128.93.8.98]) by mail1-relais-roc.national.inria.fr with SMTP; 28 Jan 2008 17:21:20 +0100 Received: by ausone.inria.fr (sSMTP sendmail emulation); Mon, _d Jan 2008 17:20:16 +0100 From: "Nicolas Pouillard" Content-Type: text/plain; charset=UTF-8 Cc: caml-list Subject: Re: [Caml-list] [OSR] OCaml Standard Recommandation Process To: "christophe.troestler+ocaml" References: <200801281204.00689.jon@ffconsultancy.com> <479DE545.9050306@janestcapital.com> <200801281525.12651.jon@ffconsultancy.com> <20080128.170610.265034237530039453.Christophe.Troestler+ocaml@umh.ac.be> In-Reply-To: <20080128.170610.265034237530039453.Christophe.Troestler+ocaml@umh.ac.be> Date: Mon, 28 Jan 2008 17:20:16 +0100 Message-Id: <1201537038-sup-3774@ausone.inria.fr> User-Agent: Sup/git X-Miltered: at discorde with ID 479E0100.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocaml:01 christophe:01 troestler:01 ocaml's:01 translated:01 cheers:01 wrote:01 caml-list:01 binding:02 binding:02 external:03 deallocate:03 let:03 let:03 scope:04 Excerpts from christophe.troestler+ocaml's message of Mon Jan 28 17:06:10 +0100 2008: > On Mon, 28 Jan 2008 15:25:12 +0000, Jon Harrop wrote: > > > > So you write a "use" binding: > > > > let read_first_line file = > > use ch = open_in file in > > input_line ch > > > > and it gets translated into: > > > > let read_first_line file = > > let ch = open_in file in > > try input_line ch finally > > ch#dispose > > > > where the "dispose" method that was automatically inserted at the end of the > > scope of the "use" binding calls "close_in" in this case to deallocate the > > external resource (a file handle in this case but it could be anything with a > > dispose method). > > What is wrong with > > let read_first_line file = > with_open_in file begin fun ch -> > input_line ch > end This code has nothing wrong but with_open_in (even if it's very useful) is not resource safe. This is because the in_channel can escape it's scope. | let ch = with_open_in file (fun x -> x) in | input_line ch This program is obviously wrong but still show that with_open_in is not safe. Cheers, -- Nicolas Pouillard aka Ertai