caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: caml-list@inria.fr
Subject: Re: [Caml-list] exn vs option
Date: Wed, 11 Apr 2012 12:26:25 +0200	[thread overview]
Message-ID: <87vcl6d0oe.fsf@frosties.localnet> (raw)
In-Reply-To: <91EB03E665BB4D7BB3CC65466956C79B@erratique.ch> ("Daniel Buenzli"'s message of "Thu, 5 Apr 2012 11:50:17 +0200")

Daniel Bünzli <daniel.buenzli@erratique.ch> writes:

> Le jeudi, 5 avril 2012 à 11:05, Goswin von Brederlow a écrit :
>> If you are writing a module then consider providing both flavours for
>> functions, one with exceptions and one with options. Even if you only do
>> something like this:
>
>
> I don't think it's a rule that should be applied consistently, I don't like libraries that provide tons of convenience functions, too many names. If you stick with the option type (or variant cases) the user can quickly wrap the function if it needs an exception (or vice-versa but I prefer it that way since it's then documented by the function signature and is in my opinion better behaved in general).
>
> 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.

Otherwise submodules are usefull:

module Foo = Foo.Options
or
module Foo = Foo.Exceptions

Foo.do_things ()

or

open Foo.Options
do_things ()

The option and exception raising functions would be named the same and
the user selects one or the other by selecting a submodule.

As said, it is usualy a matter of taste and the users tastes might
differ from the module authors taste. By providing an option and
execption flavour of a function (by different names or submodules hardly
matters) the user can use what he likes as opposed to what the modules
author imposes on him.

MfG
        Goswin

  reply	other threads:[~2012-04-11 10:26 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 [this message]
2012-04-11 10:32       ` David House
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=87vcl6d0oe.fsf@frosties.localnet \
    --to=goswin-v-b@web.de \
    --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).