caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Pierrick Couderc <pierrick.couderc@ocamlpro.com>
To: Goswin von Brederlow <goswin-v-b@web.de>
Cc: Ocaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Destructive use of file descriptors
Date: Thu, 6 Aug 2015 11:49:41 +0200	[thread overview]
Message-ID: <CA+gYG2tH7+Mn4sGQX4B4AK=GtUrjgPS1tdybxiNLRyncBy360w@mail.gmail.com> (raw)
In-Reply-To: <20150806094030.GE16477@frosties>

[-- Attachment #1: Type: text/plain, Size: 1074 bytes --]

Using higher order functions should be enough, I think?

I personnaly use this one when opening files (input in that case):

type 'a res = Ok of 'a | Error of exn
let safe_open_in f file =
  try
    let ic = open_in file in
    let r = try Ok (f ic) with e -> Error e in
    close_in ic;
    r
  with e -> Error e



2015-08-06 11:40 GMT+02:00 Goswin von Brederlow <goswin-v-b@web.de>:

> 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
>

[-- Attachment #2: Type: text/html, Size: 1994 bytes --]

      parent reply	other threads:[~2015-08-06  9:50 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
2015-08-06  9:49 ` Pierrick Couderc [this message]

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='CA+gYG2tH7+Mn4sGQX4B4AK=GtUrjgPS1tdybxiNLRyncBy360w@mail.gmail.com' \
    --to=pierrick.couderc@ocamlpro.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).