Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Richard Jones <rich@annexia.org>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] assertions or exceptions?
Date: Thu, 15 Jul 2004 11:18:26 +0100	[thread overview]
Message-ID: <20040715101826.GA10062@redhat.com> (raw)
In-Reply-To: <7f8e92aa04071501035091cbe9@mail.gmail.com>

On Thu, Jul 15, 2004 at 11:03:24AM +0300, Radu Grigore wrote:
> So, now a few questions:
> 1. Is my impression that OCaml standard library is abusing
> exceptions correct?

There's always a conflict between raising a Not_found exception and
returning an 'a option result.  Example (from mod_caml).  Should the
function which returns the hostname field from the request_rec
structure be prototyped as:

Apache.Request.hostname : Apache.Request.t -> string
  # could throw Not_found if the C string is NULL.

or:

Apache.Request.hostname : Apache.Request.t -> string option
  # returns Some string, or None if the C string is NULL

In fact, I chose the former (throwing Not_found exception)
consistently throughout mod_caml.

The reason is that returning 'a option forces you to deal with the
'None' case every time you use (eg). Apache.Request.hostname.  But in
many cases, the underlying C fields being NULL is an internal error
for which we are not prepared, and the only sensible thing to do then
is not to force people to deal with it locally, but to throw an
exception which causes the whole script/program to fail.

However in some cases the right thing to do would be to return 'a
option, precisely *because* you require the programmer to deal with
the error locally.

> 2. Is it safe to assume that exceptions complicate functional
> programs as much as they complicate imperative ones?

Perhaps in theory, but in practice they work just fine.

> 3. Is it possible to avoid using exceptions and read a text file
> line-by-line until EOF?

There are functions in ExtLib to do this, I think.  If not then you
should write a function like 'input_all_lines' or 'iter_over_lines f'
once, put it into a small local library, and use it every time.  No
point reinventing wheels each time.

Rich.

-- 
Richard Jones. http://www.annexia.org/ http://www.j-london.com/
Merjis Ltd. http://www.merjis.com/ - improving website return on investment
'There is a joke about American engineers and French engineers. The
American team brings a prototype to the French team. The French team's
response is: "Well, it works fine in practice; but how will it hold up
in theory?"'

-------------------
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-15 10:18 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 [this message]
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
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=20040715101826.GA10062@redhat.com \
    --to=rich@annexia.org \
    --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