caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jeremy Yallop <yallop@gmail.com>
To: Goswin von Brederlow <goswin-v-b@web.de>
Cc: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] Proposal: extend try to handle success
Date: Thu, 6 Feb 2014 11:36:29 +0000	[thread overview]
Message-ID: <CAAxsn=Gm7G1jcVgj5h-Fqp069gMz0hbzFNsoJ-2ngkGtm3tT_A@mail.gmail.com> (raw)
In-Reply-To: <20140206105811.GD28534@frosties>

On 6 February 2014 10:58, Goswin von Brederlow <goswin-v-b@web.de> wrote:
> Examples from the blog:
[...]
> 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'

Yes, it's essentially the same if your streams are monomorphic.
However, if MyStream.t has a type parameter then you'll need something
more like this:

  let rec iter_stream : type a. (a -> unit) -> a MyStream.t -> unit =
    fun f s ->
      let module M = struct exception Success of (a * a MyStream.t) end in
      try raise (M.Success (MyStream.get s)) with
      | End_of_stream -> ()
      | M.Success (x, s') ->
        f x;
        iter_stream f s'

The code becomes quite cluttered with irrelevant details, but things
can get even worse.  In some cases there's no way to write down the
type of the expression at all, which makes defining the exception
rather tricky.

> 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

Change '()' to '0' and your first three guesses are correct.  The last
function behaves as follows:

   # #use "f4.ml";;
   File "f4.ml", line 2, characters 2-63:
   Warning 8: this pattern-matching is not exhaustive.
   Here is an example of a value that is not matched:
   None
   val f4 : 'a -> int = <fun>
   # f4 ();;
   Exception: Match_failure ("f4.ml", 2, 2).

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

Right.  The behaviour of '_' is unchanged: it matches any exception.
The pattern 'val _' matches any value.

      parent reply	other threads:[~2014-02-06 11:36 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
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 [this message]

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='CAAxsn=Gm7G1jcVgj5h-Fqp069gMz0hbzFNsoJ-2ngkGtm3tT_A@mail.gmail.com' \
    --to=yallop@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=goswin-v-b@web.de \
    /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).