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

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