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 606AF7EE89 for ; Thu, 26 Oct 2017 09:31:43 +0200 (CEST) Authentication-Results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=paurkedal@gmail.com; spf=Pass smtp.mailfrom=paurkedal@gmail.com; spf=None smtp.helo=postmaster@mail-wm0-f52.google.com Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of paurkedal@gmail.com) identity=pra; client-ip=74.125.82.52; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="paurkedal@gmail.com"; x-sender="paurkedal@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of paurkedal@gmail.com designates 74.125.82.52 as permitted sender) identity=mailfrom; client-ip=74.125.82.52; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="paurkedal@gmail.com"; x-sender="paurkedal@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-wm0-f52.google.com) identity=helo; client-ip=74.125.82.52; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="paurkedal@gmail.com"; x-sender="postmaster@mail-wm0-f52.google.com"; x-conformance=sidf_compatible IronPort-PHdr: =?us-ascii?q?9a23=3AqR5CdRVEV/WTggVoczDtHiO034TV8LGtZVwlr6E/?= =?us-ascii?q?grcLSJyIuqrYZhSBt8tkgFKBZ4jH8fUM07OQ6P+wHzFYqb+681k8M7V0Hycfjs?= =?us-ascii?q?sXmwFySOWkMmbcaMDQUiohAc5ZX0Vk9XzoeWJcGcL5ekGA6ibqtW1aMlzFOAF0?= =?us-ascii?q?PuX4HJLJx4Tyjrjqus6bXwIdzgC8arN7Nhn+kkP08IE0ho1vMex5ngHIqXtgcu?= =?us-ascii?q?JQyHNpLBSUhRmqtemq+5s2uRxdp+4s7YZpF+3HZac1QbVcRnxyK2ko/s7muRDH?= =?us-ascii?q?ZQSK73oYFG4Rl0wbUED+8BjmU8Kp4WPBve1n1XzfZJWuQA=3D=3D?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0BMAAATj/FZhjRSfUpbGgEBAQECAQEBA?= =?us-ascii?q?QgBAQEBFAEBAQEBAQEBAQEBBwEBAQEBhBhuJweDc5kqgXqJPo8TCiWEAQFFTwK?= =?us-ascii?q?EbQdDFAEBAQEBAQEBAQEBEgEBAQgLCwgoL4I4BQEeAQWCOwEBAQECASMEGQEbH?= =?us-ascii?q?QEDAQsGBQsNAgImAgIiAREBBQEcBhOKBwEDDQgQnCtAjAyBbRgFARyDCQWDXgo?= =?us-ascii?q?ZJw1YglcBAQEBBgEBAQEcAgEFEn2CH4IHhmOIGYJhBaF5AodjjRSTKpV0FAUfg?= =?us-ascii?q?RU2ggl6XjWCL4JNH4F1PzaKGIFVAQEB?= X-IPAS-Result: =?us-ascii?q?A0BMAAATj/FZhjRSfUpbGgEBAQECAQEBAQgBAQEBFAEBAQE?= =?us-ascii?q?BAQEBAQEBBwEBAQEBhBhuJweDc5kqgXqJPo8TCiWEAQFFTwKEbQdDFAEBAQEBA?= =?us-ascii?q?QEBAQEBEgEBAQgLCwgoL4I4BQEeAQWCOwEBAQECASMEGQEbHQEDAQsGBQsNAgI?= =?us-ascii?q?mAgIiAREBBQEcBhOKBwEDDQgQnCtAjAyBbRgFARyDCQWDXgoZJw1YglcBAQEBB?= =?us-ascii?q?gEBAQEcAgEFEn2CH4IHhmOIGYJhBaF5AodjjRSTKpV0FAUfgRU2ggl6XjWCL4J?= =?us-ascii?q?NH4F1PzaKGIFVAQEB?= X-IronPort-AV: E=Sophos;i="5.43,434,1503352800"; d="scan'208";a="242412768" Received: from mail-wm0-f52.google.com ([74.125.82.52]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 26 Oct 2017 09:31:32 +0200 Received: by mail-wm0-f52.google.com with SMTP id y83so952530wmc.4 for ; Thu, 26 Oct 2017 00:31:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=cpZ06SXFPaB6JVs/e3RCXEUn3kJSOp4CBu7GOeqtG78=; b=E6IzdS2ZF2fkk0znLc5D3FS2cYKsRRLbldDUk/i3Qo9MpcB5C2urZ104jxCopOK7kr NXVBPYTAa52fskUZrRYgXPD0Ga/c1JRjVxKDkJOWL937poPMV8GYR4/lFEB8ST8Pj+ft l5YLmr1S2UkpH3Rz3ZuDnoUZ6Dz9BfqB5jWn0kO7DgHVYIPvwabQtGoH6ztcFBaolSKF AEJHYBEtFZADwfypJn1AmmaGPNCX1z7PHSgV8GMTJWJcjn0zQAwgzcjJldz9OxzB0T8w axWICfOzE8rCNCEtmVVPo0BsxifA9YfYkTU3K1FnnaqmrixwA1AVOShYfxHK7pzw6rXV 4B4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=cpZ06SXFPaB6JVs/e3RCXEUn3kJSOp4CBu7GOeqtG78=; b=HB4QOyxYddWaWUOc3K5SdvMEuqRAnHhoozxk3YPH+yyusEBiXlYYPZkOOr7nKS+Sv2 ZE/TUPH07LSgrpGwjAKEaNgSA0sFn5lIJKyRYz86nYVPEYr8HC0l7UynNxG2ep+njWL0 lVtqeih94VYSXWsliqcBkkPbmIyWIrI3ab36syG6lZChFgDN3zErQho74ClEMaej6oDa F7pqmv1QV6CthWCJnzwND5AKvI+0ZFnmNxeNHsRxGB4z/FBQnXqIPNLd13y3gKXGXC8N luPSRnoQ70MyAePBtfd7Bk5SS4XPrHkYwCTueQ8R02kqo05Dg9dSZQueIwGaQ/z8P+8q x4Wg== X-Gm-Message-State: AMCzsaW0Xe11+VMpz+6xQDhlZIa0ON6D1vMhp82oQ/sE9QAiNw0YXksl ucbal09gr+1CDbbRflnznfJDzlVJ9fC9nAlnlhw= X-Google-Smtp-Source: ABhQp+QwlwRze5rCO1fptUq5QPZubVn0hoOsCEEIi5EE/FX2TXBhLDEJ8O4G/ZxyFnDSizTJmbcSNwZisQmLJA0N1lE= X-Received: by 10.28.48.143 with SMTP id w137mr742043wmw.3.1509003092402; Thu, 26 Oct 2017 00:31:32 -0700 (PDT) MIME-Version: 1.0 Received: by 10.223.156.209 with HTTP; Thu, 26 Oct 2017 00:31:31 -0700 (PDT) In-Reply-To: <20171025083530.gvggcenrgxolduse@annexia.org> References: <86o9p2ywgc.fsf@gmail.com> <20171020113815.GD32138@nunchakus.loria.fr> <20171024133026.uovvzdbnamnzoknv@annexia.org> <20171025083530.gvggcenrgxolduse@annexia.org> From: "Petter A. Urkedal" Date: Thu, 26 Oct 2017 09:31:31 +0200 Message-ID: To: "Richard W.M. Jones" Cc: ptoscano@redhat.com, caml users Content-Type: text/plain; charset="UTF-8" Subject: Re: [Caml-list] What if exn was not an open type? On 25 October 2017 at 10:35, Richard W.M. Jones wrote: > On Tue, Oct 24, 2017 at 09:00:05PM +0200, Petter A. Urkedal wrote: >> let with_return (type b) f = >> let exception Return of b in >> try f (fun y -> raise (Return y)) with Return y -> y > 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! *) > ... > ) Using the above trick, this might be written in more C-like style as: let f () = with_return @@ fun return -> if some_problem then ( printf "sorry, can't do that\n"; return () ); if some_other_problem then ( printf "sorry, can't do that either\n"; return () ); let v = match something with | None -> return () | Some v -> v in (* finally we get to do some work! *) ... However, I would have written it: let f () = if some_problem then printf "sorry, can't do that\n" else if some_other_problem then printf "sorry, can't do that either\n" else (match something with | None -> (* return *) | Some v -> (* finally we get to do some work! *) ...) That is, treating the if ... else more like let ... in. However any ; would chop of the bodies of the conditionals, so this style only works when semicolons are parenthesised or rewritten as let () = ... in. Especially, the latter must be used if one wants to skip indenting an else block, but insert a debug statement: let f () = if some_problem then printf "sorry, can't do that\n" else if some_other_problem then printf "sorry, can't do that either\n" else let () = printf "no problems\n" in (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. Given the change of formatting above, also changes the perception: The issue now is not that the return is missing, but that one cannot *not* return from a conditional. Petter