caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Ben Millwood <bmillwood@janestreet.com>
To: Philippe Veber <philippe.veber@gmail.com>
Cc: "Daniel Bünzli" <daniel.buenzli@erratique.ch>,
	"Romain Bardou" <romain.bardou@inria.fr>,
	"caml users" <caml-list@inria.fr>
Subject: Re: [Caml-list] Uncaught exceptions in function type.
Date: Tue, 27 May 2014 09:42:35 +0100	[thread overview]
Message-ID: <CA+MHO51eaOHNWJH76hWJj2WG-YXdOVpDNWcVorMmfEwSquK6dw@mail.gmail.com> (raw)
In-Reply-To: <CAOOOohTGzRtDSuro998UMQOdvivL8E6C7Znnu=97+g8iHTUQ1A@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 3770 bytes --]

On 26 May 2014 17:02, Philippe Veber <philippe.veber@gmail.com> wrote:
>
> Using options or Result from time to time is certainly a nice habit.
> However to be honest, using option/result monads extensively does not seem
> right to me, like pulling the language in a direction it was not really
> designed for. Monad combinators do a great job at propagating errors, but
> still there is a serious addition of plumbing in function body's and Result
> returning functions have a significantly less readable type. But I guess
> this is pretty much a question of taste/habit to see if the added verbosity
> is worth it.
>

It's true that using a monadic style does introduce *some* noise, but I
don't think it's so bad, especially if you design your API to fit with the
style. In a way I almost prefer having the binds there so that I can easily
see where the possible interruption points in the flow are, and in
particular it's /syntactically/ obvious where errors /can't/ occur.

BTW core still uses exceptions. Is there an explicit rule as to how to
> decide between Result type or exceptions. For instance, why not write the
> Array.create function like this:
>
> val create : int -> 'a -> 'a array Or_error.t
>
> where create fails for a negative integer?
>

Daniel's summary is very good, although we do use Failure in Core, often
for assertion-failure like scenarios or just any case where the error isn't
meant to be handled specifically. In many cases Invalid_argument would
arguably be more appropriate, but in other cases that isn't obvious (say if
you mutate a Queue.t while you are folding over it, that will be detected
and an error will be thrown, but it isn't /really/ an invalid argument
error), and in any case since we often don't intend to catch them except to
print a stack trace and die, it doesn't matter too much.

We also do just break our own rules sometimes. Possible reasons why are:
- some aspect of usability takes priority over dogmatics (although this is
much rarer than you'd think)
- legacy exceptions (see below)
- sometimes we make bad decisions :(

Having no experience on large projects, it is not clear to me why
> exceptions are considered so dangerous. One point I see is that when you
> decide to handle errors with a try ... with expression, there is no
> exhaustivity check to ensure you handled all possible cases, and that you
> have to rely on (possibly incomplete) documentation and reasonning to
> convince yourself you forgot nothing. Another related one is that there is
> no automatic analysis to show exceptions thrown by a function in the
> documentation. These are of course already strong reasons why not to use
> too many exceptions (and were the motivation for my initial question). Do
> you see other reasons?
>

One particularly galling aspect of exceptions not showing up in types is
that they can't easily be refactored. If you decide to change which
exceptions a given function throws, you can't rely on the typechecker to
flag up where code was written to the old specification and needs changing.
You can't even just eyeball the code to see where the function was used and
see if the exception was caught, because who knows where in the call stack
it might have been handled? Explicit error types, on the other hand, will
refuse to compile unless you have changed your code to deal with it, so you
can make your errors richer or more precise with a high level of confidence
that you haven't introduced any "holes" for exceptional conditions to sneak
out where previously they couldn't.

This is essentially why we still have a few Not_found exceptions in Core,
because it's really pretty hard to know where they might be relied upon, so
it's easier to leave them in than purge them and risk silent breakage.

[-- Attachment #2: Type: text/html, Size: 4865 bytes --]

  reply	other threads:[~2014-05-27  8:42 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-26 14:23 Philippe Veber
2014-05-26 14:56 ` Romain Bardou
2014-05-26 15:13   ` Ben Millwood
2014-05-26 16:02     ` Philippe Veber
2014-05-26 16:34       ` Daniel Bünzli
2014-05-27  6:52         ` Philippe Veber
2014-05-27  8:42           ` Ben Millwood [this message]
2014-05-27 10:05             ` Goswin von Brederlow
2014-05-27 10:36               ` Ben Millwood
2014-05-27 11:24                 ` Yaron Minsky
2014-05-27 21:42             ` Daniel Bünzli
2014-05-27 21:16           ` Daniel Bünzli
2014-06-02  8:38             ` Goswin von Brederlow
2014-05-27  8:49         ` Goswin von Brederlow
2014-05-27  8:56           ` David House
2014-05-27 21:39           ` Daniel Bünzli
2014-06-02  8:31             ` Goswin von Brederlow
2014-05-27  9:25         ` Nicolas Boulay
2014-05-27 21:51           ` Daniel Bünzli
2014-05-30 18:03         ` Florian Weimer
2014-05-31 11:26           ` Daniel Bünzli
2014-06-02  8:43             ` Goswin von Brederlow
2014-05-26 15:25   ` Philippe Veber
2014-05-27  9:28     ` Goswin von Brederlow
2014-05-27  9:38       ` Romain Bardou
2014-05-26 15:33 ` Thomas Blanc
2014-05-26 16:04   ` Philippe Veber
2014-05-26 15:33 ` Gabriel Scherer

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=CA+MHO51eaOHNWJH76hWJj2WG-YXdOVpDNWcVorMmfEwSquK6dw@mail.gmail.com \
    --to=bmillwood@janestreet.com \
    --cc=caml-list@inria.fr \
    --cc=daniel.buenzli@erratique.ch \
    --cc=philippe.veber@gmail.com \
    --cc=romain.bardou@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).