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