From: "Petter A. Urkedal" <paurkedal@gmail.com>
To: "Richard W.M. Jones" <rich@annexia.org>
Cc: ptoscano@redhat.com, caml users <caml-list@inria.fr>
Subject: Re: [Caml-list] What if exn was not an open type?
Date: Thu, 26 Oct 2017 09:31:31 +0200 [thread overview]
Message-ID: <CALa9pHQ_UsAyVdGzPdsLeDcOaid-P_5xJAQmczDU03XHf-ZqaA@mail.gmail.com> (raw)
In-Reply-To: <20171025083530.gvggcenrgxolduse@annexia.org>
On 25 October 2017 at 10:35, Richard W.M. Jones <rich@annexia.org> 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
next prev parent reply other threads:[~2017-10-26 7:31 UTC|newest]
Thread overview: 44+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-10-20 9:56 Malcolm Matalka
2017-10-20 10:55 ` David Allsopp
2017-10-20 11:21 ` Ivan Gotovchits
2017-10-20 11:38 ` Simon Cruanes
2017-10-20 16:54 ` Malcolm Matalka
2017-10-20 19:47 ` Simon Cruanes
2017-10-21 21:15 ` Malcolm Matalka
2017-10-24 13:30 ` Richard W.M. Jones
2017-10-24 19:02 ` Petter A. Urkedal
2017-11-04 18:44 ` Richard W.M. Jones
2017-11-04 18:48 ` SP
2017-11-04 18:53 ` Richard W.M. Jones
2017-11-04 19:03 ` SP
2017-11-04 19:01 ` Max Mouratov
2017-11-04 19:16 ` octachron
2017-11-05 17:41 ` Richard W.M. Jones
2017-11-05 18:39 ` Yaron Minsky
2017-11-05 20:49 ` Gabriel Scherer
2017-11-05 21:48 ` Yaron Minsky
2017-11-05 21:53 ` Petter A. Urkedal
2017-11-05 18:02 ` Petter A. Urkedal
2017-11-05 18:24 ` Richard W.M. Jones
2017-11-05 18:55 ` Petter A. Urkedal
[not found] ` <CALa9pHQ-nhWf4T0U5gDiKTduPiEeXSZPQ=DY6N1YNbCXqRohPQ@mail.gmail.com>
2017-10-25 8:35 ` Richard W.M. Jones
2017-10-25 9:12 ` Philippe Veber
2017-10-25 14:52 ` Richard W.M. Jones
2017-10-25 16:37 ` Ivan Gotovchits
2017-10-25 17:47 ` SP
2017-10-26 8:06 ` Malcolm Matalka
2017-10-26 8:11 ` Xavier Leroy
2017-10-25 13:36 ` Ivan Gotovchits
2017-10-26 7:31 ` Petter A. Urkedal [this message]
2017-10-27 13:58 ` Oleg
2017-10-27 14:24 ` Philippe Veber
2017-10-27 14:49 ` Leo White
2017-11-01 7:16 ` Oleg
2017-11-04 17:52 ` Philippe Veber
2017-10-20 17:07 ` Malcolm Matalka
2017-10-21 21:28 ` Nathan Moreau
2017-10-22 12:39 ` Malcolm Matalka
2017-10-22 13:08 ` Nathan Moreau
2017-10-24 11:11 ` SP
2017-10-24 11:16 ` Gabriel Scherer
2017-10-25 11:30 ` Malcolm Matalka
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=CALa9pHQ_UsAyVdGzPdsLeDcOaid-P_5xJAQmczDU03XHf-ZqaA@mail.gmail.com \
--to=paurkedal@gmail.com \
--cc=caml-list@inria.fr \
--cc=ptoscano@redhat.com \
--cc=rich@annexia.org \
/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