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] Proposal: extend try to handle success
Date: Thu, 6 Feb 2014 11:58:12 +0100	[thread overview]
Message-ID: <20140206105811.GD28534@frosties> (raw)
In-Reply-To: <CAAxsn=HZF+=-BCA+f3aU91tpAbOAG6wdCJW2CichbV4NsT4ncA@mail.gmail.com>

On Tue, Feb 04, 2014 at 05:00:45PM +0000, Jeremy Yallop wrote:
> The recent thread about the representation of options highlighted a
> shortcoming in the "try" construct: there isn't a convenient way to
> express code that should run when the body of the "try" doesn't raise
> an exception.

In python one can write:

    try:
        x = 1
    except Exception:
        x = 2
    else:
        x = 3
    finally:
        x = 4

The "else:" block is executed when no exception is thrown and the
"finally:" block is executed in both cases.

The "finally:" block is always executed even if an exception is thrown
that isn't caught. It is the same as adding another try block around
it, catching any exception, exectuing the block and then rethrowing
the execption.

    try:
        try:
            "0"[5] = 'x'
        finally:
            print("finally")
    except Exception as exn:
        print("Got \"{0}\"".format(exn))

gives:

finally
Got "'str' object does not support item assignment"

I guess in ocaml one would write:

    type ('a, 'b) either = Success of 'a | Failure of 'b

    let res = try Success (f x) with End_of_stream as exn -> Failure exn in
    let res =
        try
            match res with
            | Failure exn -> Printf.printf "failed\n"
            | Success x -> Printf.printf "success\n"
        with exn -> Failure exn
    in
    Printf.printf "finally\n";
    match res with
    | Failure exn -> raise exn
    | Success x -> x

Could a "finally:" be added to the try syntax in some way?
 
> I'd like to propose extending OCaml with a design once suggested by
> Christophe Raffalli which elegantly handles this case.  The details,
> along with an implementation that you can try out, are in the
> following blog post:
> 
>     http://ocamllabs.github.io/compiler-hacking/2014/02/04/handler-case.html
> 
> Feedback welcome!
> 
> Jeremy

Examples from the blog:

Exceptions can be wrapped in option types to preserve tail
recursiveness:

let rec iter_stream f s =
  match (try Some (MyStream.get s) with End_of_stream -> None) with
  | None -> ()
  | Some (x, s') ->
      f x;
      iter_stream f s'

The proposed syntax would add a new keyword to match against the
result of the try block if it throws no exception:

let rec iter_stream f s =
  try MyStream.get s
  with
  | End_of_stream -> ()
  | val (x, s') ->
      f x;
      iter_stream f s'

Isn't that the same as this?

exception Success of (char * MyStream.t)
let rec iter_stream f s =
  try raise (Success (MyStream.get s))
  with
  | End_of_stream -> ()
  | Success (x, s') ->
      f x;
      iter_stream f s'

And what does this do?

let f1 x =
  try None
  with
  | Empty -> ()
  | val Some x -> x
  | val _ -> 1

let f2 x =
  try raise Not_found
  with
  | Empty -> ()
  | val Some x -> x
  | val _ -> 1

let f3 x =
  try raise Not_found
  with
  | Empty -> ()
  | val Some x -> x
  | _ -> 1

let f4 x =
  try None
  with
  | Empty -> ()
  | val Some x -> x
  | _ -> 1

My guesses:
- f1 returns 1
- f2 throws Not_found
- f3 returns 1
- f4 returns 1

How do you match any exception but not a successfull value?

let f5 x =
  try None
  with
  | Empty -> ()
  | val x -> x
  | _ -> 1

Or does "_" only match exceptions and "val _" any value?

MfG
	Goswin

  parent reply	other threads:[~2014-02-06 10:58 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-04 17:00 Jeremy Yallop
2014-02-04 17:14 ` Simon Cruanes
2014-02-04 18:09   ` Jeremy Yallop
2014-02-04 19:05     ` Gabriel Scherer
2014-02-04 19:18 ` Markus Mottl
2014-02-04 19:29   ` Markus Mottl
2014-02-04 19:42     ` Yaron Minsky
2014-02-05 16:04       ` Jesper Louis Andersen
2014-02-06 10:58 ` Goswin von Brederlow [this message]
2014-02-06 11:10   ` Ben Millwood
2014-02-10  8:47     ` Goswin von Brederlow
2014-02-10  9:23       ` Ben Millwood
2014-02-10 14:39         ` Alain Frisch
2014-02-06 11:36   ` Jeremy Yallop

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=20140206105811.GD28534@frosties \
    --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).