From: Philippe Veber <philippe.veber@gmail.com>
To: Ben Millwood <bmillwood@janestreet.com>
Cc: Romain Bardou <romain.bardou@inria.fr>, caml users <caml-list@inria.fr>
Subject: Re: [Caml-list] Uncaught exceptions in function type.
Date: Mon, 26 May 2014 18:02:15 +0200 [thread overview]
Message-ID: <CAOOOohRO8nwsNufmZ9bZbHwuNm-Xx0vO8Lkue6BcTge-egDYKQ@mail.gmail.com> (raw)
In-Reply-To: <CA+MHO51JFGxK=dmHMRbPhZguW7vJRyQ3DvfJCMh4cCa+_Y6meg@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 2265 bytes --]
Hi Ben,
2014-05-26 17:13 GMT+02:00 Ben Millwood <bmillwood@janestreet.com>:
> First of all, it seems to be universal among all languages I've much
> experience with that there are unchecked exceptions. Your program might be
> interrupted at any point by an out of memory error or a signal or
> something, and there's just not much you can do about that. You can either
> model it in your code or not, but you can't stop it from happening. So it
> seems like the best we can hope for with typed exceptions is in addition to
> unchecked ones, to make it possible (but not required) that a function
> might declare some of the exceptions it can throw.
>
That's perfectly right.
>
> But after all exceptions are just "things I can return instead of a
> result", and lightweight sum types are already pretty good at that. E.g.
> (to use Romain's syntax)
>
> val lookup : map -> key -> value raise Not_found
>
> is pretty much just the same as:
>
> val lookup : map -> key -> value option
>
Right. This is a very good use case that I adopted thanks to core.
>
> True, exceptions get automatic propagation, but the option monad interface
> makes that pretty lightweight, and you can do a similar thing with a less
> trivial sum type if you need richer type information.
>
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.
>
> See also:
> https://blogs.janestreet.com/how-to-fail-introducing-or-error-dot-t/ for
> discussions of more ways you can make error handling both explicit and
> concise.
>
Thanks! 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?
[-- Attachment #2: Type: text/html, Size: 3469 bytes --]
next prev parent reply other threads:[~2014-05-26 16:02 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 [this message]
2014-05-26 16:34 ` Daniel Bünzli
2014-05-27 6:52 ` Philippe Veber
2014-05-27 8:42 ` Ben Millwood
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=CAOOOohRO8nwsNufmZ9bZbHwuNm-Xx0vO8Lkue6BcTge-egDYKQ@mail.gmail.com \
--to=philippe.veber@gmail.com \
--cc=bmillwood@janestreet.com \
--cc=caml-list@inria.fr \
--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