caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: Goswin von Brederlow <goswin-v-b@web.de>
Cc: caml users <caml-list@inria.fr>
Subject: Re: [Caml-list] Destructive use of file descriptors
Date: Thu, 6 Aug 2015 11:48:59 +0200	[thread overview]
Message-ID: <CAPFanBGFrwqKO=JVcBo8oJuG973YFU8yG_KLyqFVpNSYdaH5hA@mail.gmail.com> (raw)
In-Reply-To: <20150806094030.GE16477@frosties>

Many libraries provide a with_file_{in,out} function capturing this
pattern, without need for specific language support.

  BatFile.with_file_out "foo" (fun fd ->
    print_endline str
  )

On Thu, Aug 6, 2015 at 11:40 AM, Goswin von Brederlow <goswin-v-b@web.de> wrote:
> Hi,
>
> in Python one can write:
>
>     with open("foo") as fd:
>         fd.write(str)
>
> This involves some language magic that will open the file for the
> duration of the block and close it at the end. The file descriptor is
> automatically closed at a know time and not leaked or left until the
> GC gets around to cleaning it up.
>
> Has anyone constructed something like that for ocaml? Maybe with a ppx
> extension?
>
> MfG
>         Goswin
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

  parent reply	other threads:[~2015-08-06  9:49 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
2015-08-11 11:14       ` Gabriel Scherer
2015-08-11 11:32         ` Ben Millwood
2015-08-06  9:48 ` Gabriel Scherer [this message]
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='CAPFanBGFrwqKO=JVcBo8oJuG973YFU8yG_KLyqFVpNSYdaH5hA@mail.gmail.com' \
    --to=gabriel.scherer@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=goswin-v-b@web.de \
    /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).