From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTP id 6E6E67EEBF for ; Thu, 6 Aug 2015 11:50:02 +0200 (CEST) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of pierrick.couderc@ocamlpro.com) identity=pra; client-ip=212.27.42.5; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="pierrick.couderc@ocamlpro.com"; x-sender="pierrick.couderc@ocamlpro.com"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of pierrick.couderc@ocamlpro.com) identity=mailfrom; client-ip=212.27.42.5; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="pierrick.couderc@ocamlpro.com"; x-sender="pierrick.couderc@ocamlpro.com"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@smtp5-g21.free.fr) identity=helo; client-ip=212.27.42.5; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="pierrick.couderc@ocamlpro.com"; x-sender="postmaster@smtp5-g21.free.fr"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0BdAwDuLMNVlAUqG9Rbg29pBoMdqWGPY4I3hXkCgUAHTAEBAQEBARIBAQEBBwsLCR8whCQBAQMBEhEERwsFCwkCBAcaHQICIhIBBQEKEgYTEhCHdwMKCAQJmR+PP4EuPjGLP5B0Ax+FLQEBAQEBAQQBAQEBAQEci0+FBQQHgmmBQwWFaQyPDIR/gmSEdIINlhYSI4EXEQaEDm2CTAEBAQ X-IPAS-Result: A0BdAwDuLMNVlAUqG9Rbg29pBoMdqWGPY4I3hXkCgUAHTAEBAQEBARIBAQEBBwsLCR8whCQBAQMBEhEERwsFCwkCBAcaHQICIhIBBQEKEgYTEhCHdwMKCAQJmR+PP4EuPjGLP5B0Ax+FLQEBAQEBAQQBAQEBAQEci0+FBQQHgmmBQwWFaQyPDIR/gmSEdIINlhYSI4EXEQaEDm2CTAEBAQ X-IronPort-AV: E=Sophos;i="5.15,622,1432591200"; d="scan'208";a="172836853" Received: from smtp5-g21.free.fr ([212.27.42.5]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 06 Aug 2015 11:50:01 +0200 Received: from mail-la0-f46.google.com (unknown [209.85.215.46]) (Authenticated sender: pierrickcouderc) by smtp5-g21.free.fr (Postfix) with ESMTPSA id 526C4D4809D for ; Thu, 6 Aug 2015 11:50:01 +0200 (CEST) Received: by labgo9 with SMTP id go9so45417743lab.3 for ; Thu, 06 Aug 2015 02:50:00 -0700 (PDT) X-Received: by 10.152.23.234 with SMTP id p10mr727638laf.52.1438854600861; Thu, 06 Aug 2015 02:50:00 -0700 (PDT) MIME-Version: 1.0 Received: by 10.152.132.133 with HTTP; Thu, 6 Aug 2015 02:49:41 -0700 (PDT) In-Reply-To: <20150806094030.GE16477@frosties> References: <20150806094030.GE16477@frosties> From: Pierrick Couderc Date: Thu, 6 Aug 2015 11:49:41 +0200 Message-ID: To: Goswin von Brederlow Cc: Ocaml Mailing List Content-Type: multipart/alternative; boundary=089e0160bbec5e3051051ca171a0 X-Validation-by: pierrick.couderc@ocamlpro.com Subject: Re: [Caml-list] Destructive use of file descriptors --089e0160bbec5e3051051ca171a0 Content-Type: text/plain; charset=UTF-8 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 : > 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 > --089e0160bbec5e3051051ca171a0 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Using higher order functions should be enough, I think?
I personnaly use this one when opening files (input in tha= t case):

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

2015-08-06 11:40 GMT+02:00 Goswin von Brederlow <goswin-v-b@web.de&= gt;:
Hi,

in Python one can write:

=C2=A0 =C2=A0 with open("foo") as fd:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 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
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = Goswin

--
Caml-list mailing list.=C2=A0 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.inr= ia.fr/bin/caml-bugs

--089e0160bbec5e3051051ca171a0--