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=AWL 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 51ECBBC0A for ; Sun, 10 Dec 2006 07:58:56 +0100 (CET) Received: from pih-relay05.plus.net (pih-relay05.plus.net [212.159.14.132]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id kBA6wtdl004487 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO) for ; Sun, 10 Dec 2006 07:58:56 +0100 Received: from [80.229.56.224] (helo=[10.0.0.5]) by pih-relay05.plus.net with esmtp (Exim) id 1GtIeE-0008Vy-SB for caml-list@yquem.inria.fr; Sun, 10 Dec 2006 06:58:55 +0000 From: Jon Harrop Organization: Flying Frog Consultancy Ltd. To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Today's inflamatory opinion: exceptions are bad Date: Sun, 10 Dec 2006 06:56:13 +0000 User-Agent: KMail/1.9.5 References: In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200612100656.14351.jon@ffconsultancy.com> X-Miltered: at concorde with ID 457BB02F.001 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; enforces:01 backtracking:01 verbose:01 stack:01 stdin:01 recursion:01 ocaml:01 recursion:01 rewriting:01 ocaml:01 ocaml's:01 frog:98 equality:01 wrote:01 rewrite:01 On Sunday 10 December 2006 01:42, Brian Hurt wrote: > I think I've come to the conclusion that exceptions are bad. :-) > For the former, returning a variant type ('a option if nothing else) is a > better idea, for (at least) two reasons. One, the type system enforces > the requirement to actually handle the error, at the location the return > value of the function is desired. Being forced to handle errors immediately and then implement your own backtracking is not necessarily a good thing. In particular, it can make your code substantially more verbose, less clear and less efficient. > Call this on a very long file, and you'll blow stack. But if input_line > returned, say, string option (where None meant end of file), the natural > code would be: > ... > The work around to this is to basically implement the function: It is often better to resort to imperative programming in that case: let list = ref [] in (try while true do list := input_line stdin :: !list done with End_of_file -> ()); !list;; > Note that this leaves the door open to sharing a single variant type among > all the input functions: > > type 'a input = > > | Input of 'a > | End_of_file > | Read_error of string I don't want to have to handle all exceptional circumstances at every step of the computation. > There's one other use for exceptions I've seen (and done)- using them as > non-local longjmps. A classic example for this is a delete functions on a > tree structure- you recurse all the way down to the leafs and discover > that you're not deleting anything at all, what do you do? I sometimes > (too often) throw an exception to jump out of the recursion back up to the > top level. That is a productive optimisation in OCaml. The alternative would be to perform physical equality tests all the way back up the recursion just to avoid reallocating identical trees. The same optimisations crops up all over the place. Rewrite systems can do the same thing to avoid rewriting identical expressions. > Major changes to the language itself I see as unlikely in the near term. > I'm mainly putting this rant out there to a) generate discussion, b) make > people think, and c) maybe influence the design of future Ocaml libraries > and code. The F# language is related to OCaml and runs under .NET. With Microsoft's .NET, exceptions are very expensive. Consequently, there is an immediate problem converting OCaml code to F# code because it is likely to run very slowly if it uses functions like "find" and "assoc". However, I'm sure which I prefer. I am certainly used to OCaml's lightweight exceptions and use them all over the place. -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. Objective CAML for Scientists http://www.ffconsultancy.com/products/ocaml_for_scientists