caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Benedikt Grundmann <benedikt.grundmann@googlemail.com>
To: Raphael Proust <raphlalou@gmail.com>
Cc: Pierre Chopin <pierre@punchup.com>, caml-list@inria.fr
Subject: Re: [Caml-list] exn vs option
Date: Thu, 5 Apr 2012 08:53:22 +0100	[thread overview]
Message-ID: <CAMr3DCOheY+aY4c6TYTKJ=e54rC8=y-mVN2D=zY8xVbe6nLywg@mail.gmail.com> (raw)
In-Reply-To: <CAAmHUA=4hpG6hgPO4jWWhkNyxDa2so1D_8+ueCnK6DcB4dEXdA@mail.gmail.com>

That post is against maybe (aka option) as an error type as it looses
information about the error.  If you use an Either type such as Core's
Result.t that argument is invalidated.

In fact at Jane Street we try avoid the use of exceptions as they don't
force the client of the library to handle the exceptional case.  Also
exceptions don't immediately imply more information one of the most wide
spread exceptions in OCaml's stdlib is Not_found which can be a nightmare
to hunt down (in particular if for whatever reason you did not also have
a backtrace).

Cheers,

Bene

On 5 April 2012 07:45, Raphael Proust <raphlalou@gmail.com> wrote:
> Aside from performance considerations, there are semantics differences
> to take into account. This blog post explain why exceptions are
> "better" (or, more precisely, why it is not generally a good idea to
> replace exceptions by options)
> http://blog.dbpatterson.com/post/9528836599 (it is in Haskell rather
> than OCaml, but it still applies).
>
> On Wed, Apr 4, 2012 at 10:25 PM, Pierre Chopin <pierre@punchup.com> wrote:
>> Hi,
>>
>> I benchmarked two programs, in one case the main function throw an exception
>> that is caught, in the other the function returns an option that is pattern
>> matched on.
>>
>> I noticed that, whether the exception is thrown or not, the option version
>> is always faster.
>>
>> Is there any case where it makes sense, performance wise, to use exception
>> instead of 'a option ?
>>
>> test1.ml
>> ----------------------------------------------------------------------
>>
>> exception Foo
>> let f x =
>>  if x =1 then raise Foo else ()
>>
>> ;;
>>
>>  for i = 0 to 10_000_000 do
>> try
>>     f 1
>> with Foo -> ()
>> done
>> ------------------------------------------------------------------------
>> test2.ml:
>> ------------------------------------------------------------------------
>> let f x =
>>     if x=1 then None else Some ()
>>
>> ;;
>> for i = 0 to 10_000_000 do
>>     match f 1 with
>>         None -> ()
>>     |   Some s -> s
>>     done
>> ------------------------------------------------------------------------
>>
>>
>>
>> --
>> Pierre Chopin,
>> Chief Technology Officer and co-founder
>> punchup LLC
>> pierre@punchup.com
>>
>
>
>
> --
> _______
> Raphael
>
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>



-- 
Calvin: I try to make everyone's day a little more
surreal.

(From Calvin & Hobbes)


  reply	other threads:[~2012-04-05  7:53 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 [this message]
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
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='CAMr3DCOheY+aY4c6TYTKJ=e54rC8=y-mVN2D=zY8xVbe6nLywg@mail.gmail.com' \
    --to=benedikt.grundmann@googlemail.com \
    --cc=caml-list@inria.fr \
    --cc=pierre@punchup.com \
    --cc=raphlalou@gmail.com \
    /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).