> 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