caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Judicael Courant <Judicael.Courant@lri.fr>
To: Daniel de Rauglaudre <daniel.de_rauglaudre@inria.fr>
Cc: Norman Ramsey <nr@eecs.harvard.edu>, caml-list@inria.fr
Subject: A common use of try ... with
Date: Thu, 16 Dec 1999 08:45:07 +0100 (MET)	[thread overview]
Message-ID: <14424.39043.786900.267749@pc87.lri.fr> (raw)
In-Reply-To: <19991215110453.C20737@jaune.inria.fr>


[french version below]
In his message of Wed December 15, 1999, Daniel de Rauglaudre writes: 
> To resolve this problem, instead of:
> 
>      try
>        let x = exp in
>        f x
>      with Exception -> y

I noticed that this programming pattern is quite common in Caml,
i.e. you frequently want to compute an exp that may raise an
exception, then do something with the value of this expression or
handle the exception (typically read a file, then either compute
something from what you have read or handle IO errors).

Of course, you can rewrite it as you mentionned. But should not there
be a construct to handle this kind of pattern in caml ?

This new construct (let us call it tryeval) would permit us to write
things like this :

tryeval exp
val x -> f x (* this case is evaluated only when exp evaluates to a value *)
with
(* possible exceptions *)
| Exception1 -> ...
| Exception2 y -> ...
...

which is IMHO more readable and general than the workaround you
mentionned.

In fact, this new construct could even be only a generalization of the
current try with :

try e with E1 -> e1 | ...

is indeed equivalent to

tryeval e val x -> x
with
  E1 -> e1
| ...

So that there is no need for a new keyword tryeval : one could just
reuse the existing "try" keyword, generalizing this construct.


For the moment, the easiest way for implementing this new behavior is
probably syntactic sugar : the previous example would expand to :

match (try Left exp with e -> Right e) with
| Left x -> f x
| Right Exception1 -> ...
| Right (Excepion2 y) -> ...

with definition :

type ('a,'b) sum =
| Left of 'a
| Right of 'b

Is not there anything like this in the righteous syntax yet ?

[english version above]

Dans son message du mercredi 15 décembre 1999 Daniel de Rauglaudre
écrit : 
> To resolve this problem, instead of:
> 
>      try
>        let x = exp in
>        f x
>      with Exception -> y

J'ai remarqué que ce "motif" est assez courant en Caml : il arrive
fréquemment qu'on veuille calculer une expression et gérer l'exception
levée s'il y en a une ou utiliser la valeur calculée s'il n'y a pas eu
d'exception (cas typique : lecture d'un fichier, puis calcul d'une
valeur ou gestion des erreurs d'entrées-sorties).

On peut bien sûr toujours écrire l'expression de la façon que tu
mentionnes mais ne devrait-il pas y avoir une construction primitive de
Caml pour effectuer ce genre d'opération ?

Cette nouvelle construction (qu'on peut appeler tryeval) s'utiliserait
ainsi :

tryeval exp
val x -> f x (* évalué seulement si exp ne lève pas d'exception, dans
ce cas x est la valeur de exp *)
with
(* exceptions possibles *)
| Exception1 -> ...
| Exception2 y -> ...
...

C'est à mon humble avis plus lisible et général que le truc que tu
donnes.

Cette nouvelle construction pourrait même être en fait une simple
généralisation de l'actuelle construction try ... with :

try e with E1 -> e1 | ...

serait en fait juste du sucre syntaxique pour 

tryeval e val x -> x
with
  E1 -> e1
| ...

Donc pas besoin d'un nouveau mot-clé : au lieu d'appeler cette
nouvelle construction tryeval, il suffit de l'appeler try.

Une façon simple d'implanter ce nouveau comportement serait de mettre
un tout petit peu de sucre syntaxique. L'exemple donné précédemment
donnerait :

match (try Left exp with e -> Right e) with
| Left x -> f x
| Right Exception1 -> ...
| Right (Excepion2 y) -> ...

à condition d'avoir au préalable défini :

type ('a,'b) sum =
| Left of 'a
| Right of 'b

Ca n'existe pas encore dans la syntaxe righteous ?

Judicaël.
-- 
Judicael.Courant@lri.fr, http://www.lri.fr/~jcourant/
[Computing timetable constraints..................done: 0 solution(s)]




  reply	other threads:[~1999-12-16  8:14 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-12-14 19:53 my stupidity and non-tail calls Norman Ramsey
1999-12-15 10:04 ` Daniel de Rauglaudre
1999-12-16  7:45   ` Judicael Courant [this message]
1999-12-16 10:43     ` A common use of try ... with Daniel de Rauglaudre
1999-12-16 11:56     ` Christophe Raffalli
1999-12-15 16:39 ` my stupidity and non-tail calls David Brown
1999-12-16 11:40 A common use of try ... with Andrew Kennedy

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=14424.39043.786900.267749@pc87.lri.fr \
    --to=judicael.courant@lri.fr \
    --cc=caml-list@inria.fr \
    --cc=daniel.de_rauglaudre@inria.fr \
    --cc=nr@eecs.harvard.edu \
    /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).