caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Edgar Friendly <thelema314@gmail.com>
To: Pierre-Alexandre Voye <ontologiae@gmail.com>
Cc: caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] How an exception could be an argument
Date: Fri, 17 Feb 2012 13:33:41 -0500	[thread overview]
Message-ID: <CAL-jcAmUGt49ZfJcb7XR-mq7ZtzQt0idzAOA6=ddPLxL2oNk2w@mail.gmail.com> (raw)
In-Reply-To: <CANnJ5GdKgpk_E1F3B9GDS-SC3EnnvLTTOgGRgEsvbZL0S_VGag@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1619 bytes --]

Here is an example of giving an exception as an argument to a function:

let run_or ~cmd ~err = if Sys.command cmd <> 0 then raise err

and an example usage:

let config_fail = Failure ("Could not configure " ^ p.id)  in
run_or ~cmd:("sh configure" ^ config_opt) ~err:config_fail;

The problem with your code seems to be that you're passing in a result, so
the exception is being raised outside your try...with block.  OCaml's eager
evaluation means that you'll probably have to pass in a function and an
argument (or just a unit function), meaning that your <|||> will be much
uglier:

let (<|||>) (f,x) (g,y) = try f x with Nothing -> g y

((fun () -> raise Nothing), ()) <|||> ((fun str -> str), "ii")

E.

On Fri, Feb 17, 2012 at 1:16 PM, Pierre-Alexandre Voye <ontologiae@gmail.com
> wrote:

> Hello, I'm trying to implement a scala concept "partial application" in
> which one can chains pattern matching function. If the first failed, the
> second is tried.
> It seems it is impossible to give an exception as argument to a function.
>
>
>
> exception Nothing;;
>
> let (<|||>) a b = try a
> with
>
>   | Nothing ->  (try b
> with
>
>                    | Nothing -> raise
> Nothing);;
>
> val ( <|||> ) : 'a -> 'a -> 'a = <fun>
>
>

>
> (raise Nothing) <|||>
> "jj";;
>
> Exception: Nothing.
>
>
> But if I try :
> try (raise Nothing)
> with
>
>   | Nothing ->  (try
> "jj"with
>
>                    | Nothing -> raise
> Nothing);;
>
> - : string = "jj"
>
> Is there a workaround ?
>
>
> Regards,
> P-A
> --
> ---------------------
> https://twitter.com/#!/ontologiae/
> http://linuxfr.org/users/montaigne
>
>

[-- Attachment #2: Type: text/html, Size: 5284 bytes --]

  reply	other threads:[~2012-02-17 18:33 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-17 18:16 Pierre-Alexandre Voye
2012-02-17 18:33 ` Edgar Friendly [this message]
2012-02-17 18:49 ` Vincent Aravantinos
2012-02-17 19:31 ` Tiphaine Turpin

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='CAL-jcAmUGt49ZfJcb7XR-mq7ZtzQt0idzAOA6=ddPLxL2oNk2w@mail.gmail.com' \
    --to=thelema314@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=ontologiae@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).