From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 554487F7C2 for ; Thu, 6 Feb 2014 11:58:15 +0100 (CET) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of goswin-v-b@web.de) identity=pra; client-ip=212.227.15.14; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="goswin-v-b@web.de"; x-sender="goswin-v-b@web.de"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of goswin-v-b@web.de) identity=mailfrom; client-ip=212.227.15.14; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="goswin-v-b@web.de"; x-sender="goswin-v-b@web.de"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mout.web.de) identity=helo; client-ip=212.227.15.14; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="goswin-v-b@web.de"; x-sender="postmaster@mout.web.de"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AioCAAdq81LU4w8OnGdsb2JhbABZg0S5dIVQgQkWDgEBAQEBBg0JCRQogiUBAQQBJxNECwsYCSUPBSiIJAEMCAQJxFkfiVyPARaDDoEUBJgqgTOEfxKPDA X-IPAS-Result: AioCAAdq81LU4w8OnGdsb2JhbABZg0S5dIVQgQkWDgEBAQEBBg0JCRQogiUBAQQBJxNECwsYCSUPBSiIJAEMCAQJxFkfiVyPARaDDoEUBJgqgTOEfxKPDA X-IronPort-AV: E=Sophos;i="4.95,793,1384297200"; d="scan'208";a="57117607" Received: from mout.web.de ([212.227.15.14]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES128-SHA; 06 Feb 2014 11:58:15 +0100 Received: from frosties.localnet ([149.172.224.32]) by smtp.web.de (mrweb102) with ESMTPSA (Nemesis) id 0LylnX-1V7hKM3Kul-0167Pp for ; Thu, 06 Feb 2014 11:58:13 +0100 Received: from mrvn by frosties.localnet with local (Exim 4.80) (envelope-from ) id 1WBMeq-0007jB-2p for caml-list@inria.fr; Thu, 06 Feb 2014 11:58:12 +0100 Date: Thu, 6 Feb 2014 11:58:12 +0100 From: Goswin von Brederlow To: caml-list@inria.fr Message-ID: <20140206105811.GD28534@frosties> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-Provags-ID: V03:K0:Vu2KE2/AuWxae1iHEi59j0KgEthmXXdALfEWwMUCSgwM2q+kc91 FAZZ6kgM5LavlfeOOy3BuHboks4eyXY3FEz6U2rh5Vn6m2ud832QvxFIa773tMDycrR97k8 43m5U0KMMmsC53WWCPb4UAxVnD7MhB5ORaZoSwHHAIkNKNtWD2bsPCQ0evrAPOPieWZq9VS KfBmULOwFZT/GAQ1DA/fw== Subject: Re: [Caml-list] Proposal: extend try to handle success 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