caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Markus Mottl <mottl@miss.wu-wien.ac.at>
To: debourse@email.enst.fr (Benoit de Boursetty)
Cc: caml-list@inria.fr (OCAML)
Subject: Re: files & handlers...
Date: Mon, 7 Feb 2000 16:46:55 +0100 (MET)	[thread overview]
Message-ID: <200002071546.QAA14260@miss.wu-wien.ac.at> (raw)
In-Reply-To: <Pine.GSO.4.02.10002051126370.24251-100000@young.enst.fr> from "Benoit de Boursetty" at Feb 05, 2000 11:37:19 AM

> What would the language have to support in order that opened files be
> automatically closed when they get out of reach from the program? Is it
> what is called "finalization"?

Yes, this would be one solution. However, there are some drawbacks to using
finalization. In fact, even the "destructor-oriented" approach in C++
suffers from this deficiency: it is possible that the file/socket/whatever
is only closed at a much later point of time than expected.

For example, there might be a long running loop between the call to the
destructor (or lots of computation without GC-triggering allocation) before
the file is closed again.

If you want to make sure that closing happens immediately after the desired
operation, you will either have to state so explicitely (inconvenient) or
use a "higher-order" trick:

  let do_file name f =
    let file = open_in name in
    f file;
    close_in file

Then you can write something like:

  do_file "foo" (fun file -> ...)

and the file will be closed again.

If you happen to use the PCRE-library (Perl Compatibility Regular
Expression) for OCaml, there are two useful functions for such things,
namely "foreach_file" and "foreach_line".

E.g.:

  open Pcre

  let _ =
    foreach_file ["foo"; "bar"] (fun (name, file) ->
      print_endline ("Processing: " ^ name);
      foreach_line in: file (fun line ->
        print_endline ("line: " ^ line)
      )
    )

Using this, you cannot forget to close files again.

Best regards,
Markus Mottl

-- 
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl



      parent reply	other threads:[~2000-02-08  8:46 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-02-05 10:37 Benoit de Boursetty
2000-02-07 15:23 ` Stefan Monnier
2000-02-07 15:46 ` Markus Mottl [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=200002071546.QAA14260@miss.wu-wien.ac.at \
    --to=mottl@miss.wu-wien.ac.at \
    --cc=caml-list@inria.fr \
    --cc=debourse@email.enst.fr \
    /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).