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 discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 04C5CBC0B for ; Sun, 10 Dec 2006 04:35:58 +0100 (CET) Received: from wx-out-0506.google.com (wx-out-0506.google.com [66.249.82.230]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id kBA3ZuMC001990 for ; Sun, 10 Dec 2006 04:35:57 +0100 Received: by wx-out-0506.google.com with SMTP id i31so1214058wxd for ; Sat, 09 Dec 2006 19:35:56 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=r2ZucYAdzYbGnR0VwgpKgmyeTyrz7h22EyD/+lvcVpXWzgTyPdiTtz7UGrhl0u4u/0YNWGTG2JQbsHgbPCeH80Sb6PUWpDS9CskPPQrCQISviqTPtDb4IhuA35BVl9DCYmg5P4QcGfwjhbG4wYW1shAvG009xEbGxm/1U6x53bY= Received: by 10.90.50.1 with SMTP id x1mr5648672agx.1165721756375; Sat, 09 Dec 2006 19:35:56 -0800 (PST) Received: by 10.90.115.2 with HTTP; Sat, 9 Dec 2006 19:35:56 -0800 (PST) Message-ID: <875c7e070612091935q2388092dr51538ff444d0e3a6@mail.gmail.com> Date: Sat, 9 Dec 2006 22:35:56 -0500 From: "Chris King" To: "Brian Hurt" Subject: Re: [Caml-list] Today's inflamatory opinion: exceptions are bad Cc: caml-list In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: X-j-chkmail-Score: MSGID : 457B809D.000 on discorde : j-chkmail score : X : 0/20 1 0.000 -> 1 X-Miltered: at discorde with ID 457B809D.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; recursion:01 recursive:01 syntax:01 ocaml:01 o'caml:01 whereby:01 compiler:01 val:01 val:01 iter:01 uncaught:01 compiler:01 iter:01 exn:01 polymorphic:01 On 12/9/06, Brian Hurt wrote: > The second reason is that match ... with doesn't break tail recursion, > while try ... with does. This code is not tail recursive: > > let rec echo_file channel = > try > begin > let line = input_line channel in > print_string (line ^ "\n"); > echo_file channel > end > with > | End_of_file -> () > ;; I'm a big fan of Martin Jambon's "let try" syntax extension [1]. With it the above construct can be written as: let rec echo_file channel = let try line = input_line channel in print_string (line ^ "\n"); echo_file channel with End_of_file -> () and would be tail-recursive. More often than not this is how I want to write my exception handlers. > My point here is this: Ocaml is not Java (a fact we should all be > gratefull for, IMHO). Simply because Java and C++ do something, doesn't > mean that it's a good thing to do. One thing Java (sort of) gets right is keeping track of which exceptions a function can throw, making it easy to ensure that some deeply nested piece of code won't cause the entire application to die from some obscure exception. I'd love to see a similar feature in O'Caml, whereby the exceptions which a function can raise are part of its type and are inferred and checked by the compiler. For example: val List.assoc: 'a -> ('a * 'b) list -> 'b raises Not_found val List.iter: ('a -> unit raises 'b) -> 'a list -> unit raises 'b This would make it easy to spot which exceptions go uncaught in the main function of a program (perhaps the compiler could even emit a warning in this case), as well as allowing the programmer to ensure that certain pieces of code won't raise arbitrary exceptions. As an example of the latter, a stricter version of List.iter could have the type: val iter_until: ('a -> unit raises Exit) -> 'a list -> unit Of course, this system would probably require that type exn be eschewed for something closer to a polymorphic variant so that typing in 'catch' statements will work correctly. Then types such as val catch_failures: ('a -> 'b raises [Failure of string | 'c]) -> 'a -> 'b raises 'c would be possible. - Chris [1] http://martin.jambon.free.fr/extend-ocaml-syntax.html#lettry