caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Malcolm Matalka <mmatalka@gmail.com>
To: Nicolas Ojeda Bar <n.oje.bar@gmail.com>
Cc: Goswin von Brederlow <goswin-v-b@web.de>,  caml-list@inria.fr
Subject: Re: [Caml-list] Destructive use of file descriptors
Date: Thu, 06 Aug 2015 19:50:38 +0000	[thread overview]
Message-ID: <864mkcci29.fsf@gmail.com> (raw)
In-Reply-To: <CAPunWhApy98_3Oyb+FkQLr7xdETAu33YpzGk2R6m9=cPBQF2tg@mail.gmail.com> (Nicolas Ojeda Bar's message of "Thu, 6 Aug 2015 11:48:07 +0200")

Nicolas Ojeda Bar <n.oje.bar@gmail.com> writes:

> Hi,
>
> No magic needed in OCaml:
>
>   let with_open path f =
>     let oc = open_out path in
>     match f oc with
>     | x -> close_out_noerr oc; x
>     | exception e -> close_out_noerr oc; raise e
>
> and then use it as
>
>   with_open "foo" (fun oc -> output_string oc str)
>
> Cheers
>
> Nicolas
>

The weakness with this (which I'm not sure if Python offers protection)
is returning something that references the file created.  It can lead to
some confusing error messages.

  reply	other threads:[~2015-08-06 19:51 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-06  9:40 Goswin von Brederlow
2015-08-06  9:48 ` Nicolas Ojeda Bar
2015-08-06 19:50   ` Malcolm Matalka [this message]
2015-08-11  9:36     ` Goswin von Brederlow
2015-08-11 11:14       ` Gabriel Scherer
2015-08-11 11:32         ` Ben Millwood
2015-08-06  9:48 ` Gabriel Scherer
2015-08-06  9:49 ` Pierrick Couderc

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=864mkcci29.fsf@gmail.com \
    --to=mmatalka@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=goswin-v-b@web.de \
    --cc=n.oje.bar@gmail.com \
    /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;
as well as URLs for NNTP newsgroup(s).