caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jon Harrop <jon@ffconsultancy.com>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Today's inflamatory opinion: exceptions are bad
Date: Sun, 10 Dec 2006 06:56:13 +0000	[thread overview]
Message-ID: <200612100656.14351.jon@ffconsultancy.com> (raw)
In-Reply-To: <Pine.LNX.4.64.0612091823020.24657@barrayar.nyct.net>

On Sunday 10 December 2006 01:42, Brian Hurt wrote:
> I think I've come to the conclusion that exceptions are bad.

:-)

> For the former, returning a variant type ('a option if nothing else) is a
> better idea, for (at least) two reasons.  One, the type system enforces
> the requirement to actually handle the error, at the location the return
> value of the function is desired.

Being forced to handle errors immediately and then implement your own 
backtracking is not necessarily a good thing. In particular, it can make your 
code substantially more verbose, less clear and less efficient.

> Call this on a very long file, and you'll blow stack.  But if input_line
> returned, say, string option (where None meant end of file), the natural
> code would be:
> ...
> The work around to this is to basically implement the function:

It is often better to resort to imperative programming in that case:

  let list = ref [] in
  (try
     while true do
       list := input_line stdin :: !list
     done
   with End_of_file -> ());
  !list;;

> Note that this leaves the door open to sharing a single variant type among
> all the input functions:
>
> type 'a input =
>
>  	| Input of 'a
>  	| End_of_file
>  	| Read_error of string

I don't want to have to handle all exceptional circumstances at every step of 
the computation.

> There's one other use for exceptions I've seen (and done)- using them as
> non-local longjmps.  A classic example for this is a delete functions on a
> tree structure- you recurse all the way down to the leafs and discover
> that you're not deleting anything at all, what do you do?  I sometimes
> (too often) throw an exception to jump out of the recursion back up to the
> top level.

That is a productive optimisation in OCaml. The alternative would be to 
perform physical equality tests all the way back up the recursion just to 
avoid reallocating identical trees.

The same optimisations crops up all over the place. Rewrite systems can do the 
same thing to avoid rewriting identical expressions.

> Major changes to the language itself I see as unlikely in the near term.
> I'm mainly putting this rant out there to a) generate discussion, b) make
> people think, and c) maybe influence the design of future Ocaml libraries
> and code.

The F# language is related to OCaml and runs under .NET. With 
Microsoft's .NET, exceptions are very expensive. Consequently, there is an 
immediate problem converting OCaml code to F# code because it is likely to 
run very slowly if it uses functions like "find" and "assoc". However, I'm 
sure which I prefer. I am certainly used to OCaml's lightweight exceptions 
and use them all over the place.

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists


  parent reply	other threads:[~2006-12-10  6:58 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-12-10  1:42 Brian Hurt
2006-12-10  2:40 ` [Caml-list] " skaller
2006-12-10  2:51 ` Martin Jambon
2006-12-10  3:35 ` Chris King
2006-12-10  6:32   ` Jon Harrop
2006-12-10 19:07     ` brogoff
2006-12-10 18:04   ` Richard Jones
2006-12-10 23:27     ` Chris King
2006-12-11 15:55       ` Richard Jones
2006-12-15 11:13         ` Frédéric Gava
2006-12-11 17:28     ` Mike Lin
2006-12-11 20:09       ` Richard Jones
2006-12-11 23:38   ` Olivier Andrieu
     [not found]   ` <C841DA73-83D4-4CDD-BF4A-EA803C6D6A08@vub.ac.be>
2006-12-23  4:23     ` Ocaml checked exceptions Chris King
2006-12-10  6:30 ` [Caml-list] Today's inflamatory opinion: exceptions are bad malc
2006-12-10  6:36   ` malc
2006-12-10  6:56 ` Jon Harrop [this message]
2006-12-10  9:51 ` Andreas Rossberg
2006-12-10 11:00   ` Tom
2006-12-10 11:25     ` Andreas Rossberg
2006-12-10 13:27   ` Jean-Christophe Filliatre
2006-12-10 19:15     ` Haoyang Wang
2006-12-10 21:43       ` Jean-Christophe Filliatre
2006-12-11 13:10       ` Diego Olivier FERNANDEZ PONS
2006-12-10 18:31   ` Serge Aleynikov

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=200612100656.14351.jon@ffconsultancy.com \
    --to=jon@ffconsultancy.com \
    --cc=caml-list@yquem.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).