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.0 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 mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id CEB5FBC6C for ; Mon, 28 Jan 2008 23:05:43 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ah4FAA/hnUdIDszjf2dsb2JhbACCNo1nAQEJBAUIChEFgRaUOoVv X-IronPort-AV: E=Sophos;i="4.25,262,1199660400"; d="scan'208";a="21907079" Received: from qb-out-0506.google.com ([72.14.204.227]) by mail4-smtp-sop.national.inria.fr with ESMTP; 28 Jan 2008 23:05:33 +0100 Received: by qb-out-0506.google.com with SMTP id e11so880203qbe.15 for ; Mon, 28 Jan 2008 14:05:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; bh=dD5MTU1qUhr7GR96ITUsil/FTcO+PHfZchC7sd6N8T0=; b=x8sPZhusF78ykRCe2AP6dXH/o0054lU7cFxZK2pD/kyqhpad8X0X4iYJi8YxyeQrKxLsmM1dXqz7fQCc6JTF5y4hnU/QjxLE9pE6hhsXe/wIMLgeBTjOyBx9dpSxiZLvUIc2UP2kRNCkJIVfZvkRRVp70Xm/NkBn1DyS6Isf83c= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=XbfgzROStFfaKMbeACifzwrfrgpYhWpf8ck7/aFo0G5751aTx23+51bmMmj5AAne4jcFlcSRE29QwZaoPN0rfABddMMWno2jBxRI7qTKxeoDZACxv3Xela55MjVRyPj07K40ZiuMy+sVY0cjre3+tAelrSPj55F/Z0K/0NJKmMU= Received: by 10.142.203.13 with SMTP id a13mr2771509wfg.210.1201557931006; Mon, 28 Jan 2008 14:05:31 -0800 (PST) Received: by 10.142.98.15 with HTTP; Mon, 28 Jan 2008 14:05:30 -0800 (PST) Message-ID: <9d3ec8300801281405y5fc18db5sce85639dfc3dee22@mail.gmail.com> Date: Mon, 28 Jan 2008 22:05:30 +0000 From: "Till Varoquaux" To: "Jon Harrop" Subject: Re: [Caml-list] [OSR] OCaml Standard Recommandation Process Cc: caml-list@yquem.inria.fr In-Reply-To: <200801282049.08477.jon@ffconsultancy.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <200801281204.00689.jon@ffconsultancy.com> <200801281525.12651.jon@ffconsultancy.com> <20080128.170610.265034237530039453.Christophe.Troestler+ocaml@umh.ac.be> <200801282049.08477.jon@ffconsultancy.com> X-Spam: no; 0.00; ocaml:01 ocaml:01 runtime:01 variants:01 recursive:01 inria's:01 christophe:01 troestler:01 translated:01 combinators:01 val:01 val:01 combinators:01 stdlib:01 complained:01 It is worth noting that OCaml is not F#. Objects in OCaml, unlike in F#, are an advance feature, many users find them hard to grasp and work with. They also come with a rather high runtime cost. Unless we have a good reason to do so, I think we should avoid bolstering them at the very core of the standard library. Don't get me wrong, I think objects have a purpose; and so have polymorphic variants and recursive modules but great care should be taken before axing the core library around them. Out of the so called "advanced" features of OCaml only the labelled and optional arguments have made their way in the standard library. As much as we all enjoy re-inventing the standard library around a nice cold beer, we have to give credit to the INRIA's team for their collective wit and the work that actually has been done. We should keep complicated solutions for complicated problems. Although is is tempting to propose a solution for IO using phantom types, objects or whatnot, I still think the Common Lisp approach (using unwind_protect) is an elaborate enough one to avoid most problems whilst remaining simple enough to be viable. A great solution in F# might not translate as well in OCaml, or it might not translate at all. Till On Jan 28, 2008 8:49 PM, Jon Harrop wrote: > On Monday 28 January 2008 16:06:10 Christophe TROESTLER wrote: > > 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 > > Sure. Here's the complete version using combinators as a workaround: > > # let try_finally x f g = > try > let f_x = f x in > g x; > f_x > with e -> > (try g x with _ -> ()); > raise e;; > val try_finally : 'a -> ('a -> 'b) -> ('a -> unit) -> 'b = > > # let with_open_in file k = > try_finally (open_in file) k close_in;; > val with_open_in : string -> (in_channel -> 'a) -> 'a = > > # let read_first_line file = > with_open_in file begin fun ch -> > input_line ch > end;; > val read_first_line : string -> string = > > This is almost exactly what I currently do. > > The "use" binding is syntactic sugar to make it shorter, clearer, more generic > and scale better. > > I'd be more than happy to have anything at all added to improve this though. > Combinators in the stdlib are certainly better than nothing and this is one > of the most complained about noob problems (and lack of functions to read > files)... > > -- > Dr Jon D Harrop, Flying Frog Consultancy Ltd. > http://www.ffconsultancy.com/products/?e > > _______________________________________________ > > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > -- http://till-varoquaux.blogspot.com/