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 mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id 9DE247EE89 for ; Wed, 25 Oct 2017 10:35:32 +0200 (CEST) Authentication-Results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=rich@annexia.org; spf=Pass smtp.mailfrom=rich@annexia.org; spf=Pass smtp.helo=postmaster@annexia.org Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of rich@annexia.org) identity=pra; client-ip=80.68.91.176; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="rich@annexia.org"; x-sender="rich@annexia.org"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of rich@annexia.org designates 80.68.91.176 as permitted sender) identity=mailfrom; client-ip=80.68.91.176; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="rich@annexia.org"; x-sender="rich@annexia.org"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of postmaster@annexia.org designates 80.68.91.176 as permitted sender) identity=helo; client-ip=80.68.91.176; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="rich@annexia.org"; x-sender="postmaster@annexia.org"; x-conformance=sidf_compatible; x-record-type="v=spf1" IronPort-PHdr: =?us-ascii?q?9a23=3A1ub55RAefnN6EJYRRvtnUyQJP3N1i/DPJgcQr6Af?= =?us-ascii?q?oPdwSP77ocbcNUDSrc9gkEXOFd2Crakb26yL6+jJYi8p39WoiDg6aptCVhsI24?= =?us-ascii?q?09vjcLJ4q7M3D9N+PgdCcgHc5PBxdP9nC/NlVJSo6lPwWB6lX71zMZGw3+OAxp?= =?us-ascii?q?Pay1X9eK14Xkn9y1rtfrbhlUiSD1SPU6EQ+1oAnYsoNe1ZNrN7wyxxfAplNHfu?= =?us-ascii?q?1XwSVjIlfFzDjm4cLltqRi+CRZof5pyIhlF+3YeKI1VvYQWC4nOGAd7cvtuAnC?= =?us-ascii?q?RE2I/HRKATZeqQZBHwWQtEKyZZz2qCav87MlgCQ=3D?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0C7AQBzS/BZ/7BbRFBaGgEBAQECAQEBA?= =?us-ascii?q?QgBAQEBFQEBAQECAQEBAQgBAQEBgzRkbiedTz8BAQEBAQEGgS+YSwolhAEBRU8?= =?us-ascii?q?ChGhDFAEBAQEBAQEBAQEBaiiCOAUBHgEFgjwBBScTPxALGAkTEg8tGwYTihMRD?= =?us-ascii?q?KsCOosSAQEIAgEgBYMuggeBCT2FHYp6BZhziHoCh2ONA5MylX+BOTYhgVuBBwh?= =?us-ascii?q?JgmSCXByBaD82ik+BVQEBAQ?= X-IPAS-Result: =?us-ascii?q?A0C7AQBzS/BZ/7BbRFBaGgEBAQECAQEBAQgBAQEBFQEBAQE?= =?us-ascii?q?CAQEBAQgBAQEBgzRkbiedTz8BAQEBAQEGgS+YSwolhAEBRU8ChGhDFAEBAQEBA?= =?us-ascii?q?QEBAQEBaiiCOAUBHgEFgjwBBScTPxALGAkTEg8tGwYTihMRDKsCOosSAQEIAgE?= =?us-ascii?q?gBYMuggeBCT2FHYp6BZhziHoCh2ONA5MylX+BOTYhgVuBBwhJgmSCXByBaD82i?= =?us-ascii?q?k+BVQEBAQ?= X-IronPort-AV: E=Sophos;i="5.43,431,1503352800"; d="scan'208";a="242282340" Received: from annexia.org ([80.68.91.176]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/AES256-GCM-SHA384; 25 Oct 2017 10:35:31 +0200 Received: from rich by annexia.org with local (Exim 4.89) (envelope-from ) id 1e7H9u-0001cH-4Z; Wed, 25 Oct 2017 09:35:30 +0100 Date: Wed, 25 Oct 2017 09:35:30 +0100 From: "Richard W.M. Jones" To: "Petter A. Urkedal" Cc: ptoscano@redhat.com, caml-list@inria.fr Message-ID: <20171025083530.gvggcenrgxolduse@annexia.org> References: <86o9p2ywgc.fsf@gmail.com> <20171020113815.GD32138@nunchakus.loria.fr> <20171024133026.uovvzdbnamnzoknv@annexia.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20170113 (1.7.2) Subject: Re: [Caml-list] What if exn was not an open type? On Tue, Oct 24, 2017 at 09:00:05PM +0200, Petter A. Urkedal wrote: > On 24 October 2017 at 15:30, Richard W.M. Jones wrote: > > Since I first used OCaml I have wished for a simple (and type safe) > > return statement. > > It's possible to wrap a bit type (and exception) safety about > exceptions used for return: > > val with_return : (('a -> 'b) -> 'a) -> 'a > > let with_return (type b) f = > let exception Return of b in > try f (fun y -> raise (Return y)) with Return y -> y > > E.g. > > with_return (fun return -> List.iter (function 0 -> () | i -> return > i) [0; 0; 24; 0]; -1);; Thanks, that's interesting. -- As mine was a bit of a "Hit and run" comment, let me expand on why a return statement is useful for the kind of dull code that I write. I often have to write functions of the form: let f () = if some_problem then ( printf "sorry, can't do that\n"; (* return *) ) else if some_other_problem then ( printf "sorry, can't do that either\n"; (* return *) ) else ( match something with | None -> (* return *) | Some v -> (* finally we get to do some work! *) ... ) Real code is often heavily indented, or we have to put the work into awkward nested functions. This file contains a few real examples: https://github.com/libguestfs/libguestfs/blob/master/daemon/inspect_fs_unix_fstab.ml This would work a lot better with a return statement rather than workarounds. Having said all that I was writing a little ML language last year and I tried to implement a return statement, but it was very awkward to work out how to map that to my lambda calculus, so I understand how return statements are rather difficult to implement in practice. Rich. -- Richard Jones