caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: David House <dhouse@janestreet.com>
To: Goswin von Brederlow <goswin-v-b@web.de>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] exn vs option
Date: Wed, 11 Apr 2012 11:32:16 +0100	[thread overview]
Message-ID: <CAK=fH+jALJGemFegBEa1T2N76pVAjPswLyR0_5sPF4tcBUbNtw@mail.gmail.com> (raw)
In-Reply-To: <87vcl6d0oe.fsf@frosties.localnet>

On Wed, Apr 11, 2012 at 11:26 AM, Goswin von Brederlow
<goswin-v-b@web.de> wrote:
>> However in the particular case of finding something in a data structure where the user could be confronted with a situation where he can prove that the datum will be found I think it's justified to provide both flavours. For these cases, in my own code I settled on the following pattern :
>>
>>     val find : 'a t -> key -> 'a option
>>     (** [find d k] is the value bound to [k] in [d], if any. *)
>>
>>     val get : 'a t -> key -> 'a
>>     (** [get d k] is like {!find} but raises [Invalid_argument] if [k] is not bound in [d]. *)
>
> That pattern works well if you have just one or few such functions and
> can think of nice names to differentiate them.

A common pattern that is convenient and clear is to append _exn to the
functions that raise excetions on failure.

This is used all over Core, and has the advantage that it is very
obvious to client code that the call may raise an exception. A call to
an _exn function without a try/with or a clearly true invariant is an
indication that you might have a bug. (Not always; sometimes the
appropriate behaviour to crash the program when some function fails.)


  reply	other threads:[~2012-04-11 10:32 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-04 20:25 Pierre Chopin
2012-04-04 20:38 ` John Carr
2012-04-04 22:10   ` Julien Verlaguet
2012-04-05  1:29     ` Francois Berenger
2012-04-05  6:45 ` Raphael Proust
2012-04-05  7:53   ` Benedikt Grundmann
2012-04-05  9:05 ` Goswin von Brederlow
2012-04-05  9:50   ` Daniel Bünzli
2012-04-11 10:26     ` Goswin von Brederlow
2012-04-11 10:32       ` David House [this message]
2012-04-11 10:36         ` David House
2012-04-05 20:19   ` Pierre Chopin

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='CAK=fH+jALJGemFegBEa1T2N76pVAjPswLyR0_5sPF4tcBUbNtw@mail.gmail.com' \
    --to=dhouse@janestreet.com \
    --cc=caml-list@inria.fr \
    --cc=goswin-v-b@web.de \
    /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).