caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: Malcolm Matalka <mmatalka@gmail.com>
Cc: Nicolas Ojeda Bar <n.oje.bar@gmail.com>, caml-list@inria.fr
Subject: Re: [Caml-list] Destructive use of file descriptors
Date: Tue, 11 Aug 2015 11:36:25 +0200	[thread overview]
Message-ID: <20150811093625.GA5949@frosties> (raw)
In-Reply-To: <864mkcci29.fsf@gmail.com>

On Thu, Aug 06, 2015 at 07:50:38PM +0000, Malcolm Matalka wrote:
> 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.

Yeah. I was thinking that that might be avoided. Something on the line
of putting the file descriptor into a local module so ocaml would
complain about it escaping its scope or something.

At least unlike the other suggestion this one closes the FD on exception.

MfG
	Goswin

  reply	other threads:[~2015-08-11  9:36 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
2015-08-11  9:36     ` Goswin von Brederlow [this message]
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=20150811093625.GA5949@frosties \
    --to=goswin-v-b@web.de \
    --cc=caml-list@inria.fr \
    --cc=mmatalka@gmail.com \
    --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).