From: "Chris King" <colanderman@gmail.com>
To: "Brian Hurt" <bhurt@spnz.org>
Cc: caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] Today's inflamatory opinion: exceptions are bad
Date: Sat, 9 Dec 2006 22:35:56 -0500 [thread overview]
Message-ID: <875c7e070612091935q2388092dr51538ff444d0e3a6@mail.gmail.com> (raw)
In-Reply-To: <Pine.LNX.4.64.0612091823020.24657@barrayar.nyct.net>
On 12/9/06, Brian Hurt <bhurt@spnz.org> wrote:
> The second reason is that match ... with doesn't break tail recursion,
> while try ... with does. This code is not tail recursive:
>
> let rec echo_file channel =
> try
> begin
> let line = input_line channel in
> print_string (line ^ "\n");
> echo_file channel
> end
> with
> | End_of_file -> ()
> ;;
I'm a big fan of Martin Jambon's "let try" syntax extension [1]. With
it the above construct can be written as:
let rec echo_file channel =
let try line = input_line channel in
print_string (line ^ "\n");
echo_file channel
with End_of_file -> ()
and would be tail-recursive. More often than not this is how I want
to write my exception handlers.
> My point here is this: Ocaml is not Java (a fact we should all be
> gratefull for, IMHO). Simply because Java and C++ do something, doesn't
> mean that it's a good thing to do.
One thing Java (sort of) gets right is keeping track of which
exceptions a function can throw, making it easy to ensure that some
deeply nested piece of code won't cause the entire application to die
from some obscure exception. I'd love to see a similar feature in
O'Caml, whereby the exceptions which a function can raise are part of
its type and are inferred and checked by the compiler. For example:
val List.assoc: 'a -> ('a * 'b) list -> 'b raises Not_found
val List.iter: ('a -> unit raises 'b) -> 'a list -> unit raises 'b
This would make it easy to spot which exceptions go uncaught in the
main function of a program (perhaps the compiler could even emit a
warning in this case), as well as allowing the programmer to ensure
that certain pieces of code won't raise arbitrary exceptions. As an
example of the latter, a stricter version of List.iter could have the
type:
val iter_until: ('a -> unit raises Exit) -> 'a list -> unit
Of course, this system would probably require that type exn be
eschewed for something closer to a polymorphic variant so that typing
in 'catch' statements will work correctly. Then types such as
val catch_failures: ('a -> 'b raises [Failure of string | 'c]) -> 'a
-> 'b raises 'c
would be possible.
- Chris
[1] http://martin.jambon.free.fr/extend-ocaml-syntax.html#lettry
next prev parent reply other threads:[~2006-12-10 3:35 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 [this message]
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
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=875c7e070612091935q2388092dr51538ff444d0e3a6@mail.gmail.com \
--to=colanderman@gmail.com \
--cc=bhurt@spnz.org \
--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