caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: John Prevost <j.prevost@gmail.com>
To: caml-list@inria.fr
Subject: Re: [Caml-list] assertions or exceptions?
Date: Fri, 16 Jul 2004 22:55:45 -0400	[thread overview]
Message-ID: <d849ad2a040716195589b456@mail.gmail.com> (raw)
In-Reply-To: <40F82445.3070307@cs.caltech.edu>

> 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


  reply	other threads:[~2004-07-17  2:55 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-07-15  8:03 Radu Grigore
2004-07-15 10:18 ` Richard Jones
2004-07-15 10:28   ` Daniel Andor
2004-07-15 12:49   ` Radu Grigore
2004-07-15 13:33     ` Richard Jones
2004-07-15 13:58       ` Radu Grigore
2004-07-16 18:53         ` Aleksey Nogin
2004-07-17  2:55           ` John Prevost [this message]
2004-07-17 14:24             ` David MENTRE
2004-07-15 12:35 ` Jon Harrop
2004-07-15 13:45   ` Radu Grigore
2004-07-15 14:33     ` Jon Harrop
2004-07-15 15:05       ` Radu Grigore
2004-07-15 16:24     ` skaller
2004-07-15 15:38 ` [Caml-list] Unboxing options, was " Brian Hurt
2004-07-15 16:25   ` John Hughes
2004-07-15 17:00     ` Brian Hurt
2004-07-15 17:20   ` John Prevost
2004-07-15 19:14     ` Radu Grigore
2004-07-15 19:56     ` John Carr
2004-07-15 20:48       ` Brian Hurt
2004-07-15 20:49         ` John Carr
2004-07-15 21:15           ` John Prevost
2004-07-15 21:15           ` Karl Zilles
2004-07-15 21:26           ` Brian Hurt
2004-07-15 21:04       ` John Prevost
2004-07-15 21:17     ` skaller
2004-07-15 21:35       ` Brian Hurt
2004-07-15 21:51         ` skaller
2004-07-15 21:42       ` skaller
2004-07-16  0:35     ` Jacques GARRIGUE
2004-07-16  1:03       ` John Prevost
2004-07-16  2:00         ` Jacques GARRIGUE
2004-07-16 16:40         ` Xavier Leroy
2004-07-19  8:58           ` Damien Doligez

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d849ad2a040716195589b456@mail.gmail.com \
    --to=j.prevost@gmail.com \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).