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 ESMTPS id F3CEB7EEBF for ; Thu, 6 Aug 2015 11:48:07 +0200 (CEST) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of n.oje.bar@gmail.com) identity=pra; client-ip=209.85.212.181; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="n.oje.bar@gmail.com"; x-sender="n.oje.bar@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of n.oje.bar@gmail.com designates 209.85.212.181 as permitted sender) identity=mailfrom; client-ip=209.85.212.181; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="n.oje.bar@gmail.com"; x-sender="n.oje.bar@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-wi0-f181.google.com) identity=helo; client-ip=209.85.212.181; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="n.oje.bar@gmail.com"; x-sender="postmaster@mail-wi0-f181.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0BSBQDuLMNVlLXUVdFbgzo1aQaDHalhhGCLA4I3hXkCgUAHTAEBAQEBARIBAQEBBwsLCR8whCQBAQMBEhEEGQEbEgsBAwELBgMCBAcaHQICIgERAQUBChIGExIQh3YBAwoIDZkfjz+BLj4xiz+BbIJ5i0UKGScDChVChGsBAQEBAQUBAQEBAQEBFQEFDotBhQUEB4JpgUMFhWkMjwyEf4dYgg2WFhIjgRcRBoQOPDGCTAEBAQ X-IPAS-Result: A0BSBQDuLMNVlLXUVdFbgzo1aQaDHalhhGCLA4I3hXkCgUAHTAEBAQEBARIBAQEBBwsLCR8whCQBAQMBEhEEGQEbEgsBAwELBgMCBAcaHQICIgERAQUBChIGExIQh3YBAwoIDZkfjz+BLj4xiz+BbIJ5i0UKGScDChVChGsBAQEBAQUBAQEBAQEBFQEFDotBhQUEB4JpgUMFhWkMjwyEf4dYgg2WFhIjgRcRBoQOPDGCTAEBAQ X-IronPort-AV: E=Sophos;i="5.15,622,1432591200"; d="scan'208";a="172836486" Received: from mail-wi0-f181.google.com ([209.85.212.181]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 06 Aug 2015 11:48:07 +0200 Received: by wibhh20 with SMTP id hh20so16301931wib.0 for ; Thu, 06 Aug 2015 02:48:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=oEDA2hrMXe5VZ98LTEK91WFVizbFxCB7OFAhCu9zJrQ=; b=g4ljY7QEJtidhDUDcvShl1Kd49+217iFxoWOylkEIWUbiPVe58Sji3hHVwSZIA/pcw arsuTUTgLcMOWCYRkhbegyDGaE/tGCAfdV1iLbyZ1SIIv6Dmzogr5zHWoN/cXdpU7oBg DKx+vMPk1qTQNR/YXva3kpQdO9tqtD8lpgJR6+9XRY1FYq0iBglqAp5uk/EEWT5KrFD6 ISLWE0D76agbDsLNDq/odNy1OeVIyUbS9YP+W4cszqWykyREjk1M0Uq22qq9jNj8Wn5Y cbv3aYZePLk309KdMAPjBBov6hvAhtClH+RTgfO/k+jQue8xU0c4MZnIj9T9TgwztH3p RZ6w== MIME-Version: 1.0 X-Received: by 10.194.216.68 with SMTP id oo4mr1491181wjc.81.1438854487353; Thu, 06 Aug 2015 02:48:07 -0700 (PDT) Received: by 10.194.166.200 with HTTP; Thu, 6 Aug 2015 02:48:07 -0700 (PDT) In-Reply-To: <20150806094030.GE16477@frosties> References: <20150806094030.GE16477@frosties> Date: Thu, 6 Aug 2015 11:48:07 +0200 Message-ID: From: Nicolas Ojeda Bar To: Goswin von Brederlow Cc: caml-list@inria.fr Content-Type: multipart/alternative; boundary=089e0141a0929a3242051ca16a14 Subject: Re: [Caml-list] Destructive use of file descriptors --089e0141a0929a3242051ca16a14 Content-Type: text/plain; charset=UTF-8 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 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 > --089e0141a0929a3242051ca16a14 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hi,

No magic needed in OCaml= :

=C2=A0 let with_open path f =3D
=C2=A0= =C2=A0 let oc =3D open_out path in
=C2=A0 =C2=A0 match f oc with=
=C2=A0 =C2=A0 | x -> close_out_noerr oc; x
=C2=A0 = =C2=A0 | exception e -> close_out_noerr oc; raise e

=
and then use it as

=C2=A0 with_open "foo= " (fun oc -> output_string oc str)

Cheers<= /div>

Nicolas

=
On Thu, Aug 6, 2015 at 11:40 AM, Goswin von Bred= erlow <goswin-v-b@web.de> wrote:
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/ocam= l_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

--089e0141a0929a3242051ca16a14--