From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: weis Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id JAA27004 for caml-redistribution; Thu, 16 Dec 1999 09:14:48 +0100 (MET) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id IAA26385 for ; Thu, 16 Dec 1999 08:45:13 +0100 (MET) Received: from lri.lri.fr (lri.lri.fr [129.175.15.1]) by nez-perce.inria.fr (8.8.7/8.8.7) with ESMTP id IAA23699; Thu, 16 Dec 1999 08:45:11 +0100 (MET) Received: from pc87.lri.fr (pc87.lri.fr [129.175.8.106]) by lri.lri.fr (8.9.3/jtpda-5.3.2) with ESMTP id IAA29412 ; Thu, 16 Dec 1999 08:45:09 +0100 (MET) Received: by pc87.lri.fr (8.9.3/feuille) id IAA25206 ; Thu, 16 Dec 1999 08:45:08 +0100 X-Authentication-Warning: pc87.lri.fr: jcourant set sender to jcourant@pc87.lri.fr using -f From: Judicael Courant MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Message-ID: <14424.39043.786900.267749@pc87.lri.fr> Date: Thu, 16 Dec 1999 08:45:07 +0100 (MET) To: Daniel de Rauglaudre CC: Norman Ramsey , caml-list@inria.fr Subject: A common use of try ... with In-Reply-To: <19991215110453.C20737@jaune.inria.fr> References: <199912141953.OAA04635@labrador.eecs.harvard.edu> <19991215110453.C20737@jaune.inria.fr> X-Mailer: VM 6.62 under Emacs 20.3.1 Sender: weis [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)]