From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id EAA19971; Sat, 17 Jul 2004 04:55:48 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id EAA17938 for ; Sat, 17 Jul 2004 04:55:47 +0200 (MET DST) Received: from mproxy.gmail.com (rproxy.gmail.com [64.233.170.201]) by nez-perce.inria.fr (8.12.10/8.12.10) with SMTP id i6H2tkEV019449 for ; Sat, 17 Jul 2004 04:55:46 +0200 Received: by mproxy.gmail.com with SMTP id 55so56490rni for ; Fri, 16 Jul 2004 19:55:45 -0700 (PDT) Received: by 10.38.59.67 with SMTP id h67mr141078rna; Fri, 16 Jul 2004 19:55:45 -0700 (PDT) Message-ID: Date: Fri, 16 Jul 2004 22:55:45 -0400 From: John Prevost To: caml-list@inria.fr Subject: Re: [Caml-list] assertions or exceptions? In-Reply-To: <40F82445.3070307@cs.caltech.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit References: <7f8e92aa04071501035091cbe9@mail.gmail.com> <20040715101826.GA10062@redhat.com> <7f8e92aa04071505495315454@mail.gmail.com> <20040715133359.GA13273@redhat.com> <7f8e92aa040715065823a44bdf@mail.gmail.com> <40F82445.3070307@cs.caltech.edu> X-Miltered: at nez-perce with ID 40F89532.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; prevost:01 prevost:01 caml-list:01 openfile:01 openfile:01 val:01 hashtbl:01 hashtbl:01 invoke:01 bool:01 ocaml:01 assertions:01 raises:01 ought:01 catching:02 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > Actually your FileExists/OpenFile case is a perfect example of > why you need exceptions anyway - even if you test a file with > FileExists, by the time you invoke OpenFile, the file might be > already gone! So unless you want to open yourself to race > conditions, the only safe way is to go ahead and use OpenFile, > catching any exceptions it could raise. That's definitely a good example of a place where exceptions are needed. My general rule of thumb is "If it's a normal expected result, it should not be an exception. If it's an actual failure mode, it should be an exception." An example of one place in the OCaml standard libraries where I get irked is with various container data-structures where searching for an item raises "Not_found" when the item is not in the structure. From my point of view, not finding something is very much expected behavior. On the other side, I do understand why using exceptions for this case might be desirable for the implementation. And on the super double-plus good side, it's not like making wrapper functions to switch from one choice to the other is difficult. In fact, with one wrapper you can convert almost all Not_found type exceptions in the standard library to options: val find_wrap : ('a -> 'b -> 'c) -> 'a -> 'b -> 'c option let find_wrap f a b = try Some (f a b) with Not_found -> None Not all "search" type functions in the standard library take arguments in the same order, but pretty much all of them take two arguments: the data structure to search in, and something describing what to find. So the above ought to work for all three of List.assoc ('a -> ('a * 'b) list -> 'b), List.find (('a -> bool) -> 'a list -> 'a), and Hashtbl.find (('a, 'b) Hashtbl.t -> 'a -> 'b). John. ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners