From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 3DDCD7F736 for ; Fri, 18 Sep 2015 22:39:00 +0200 (CEST) IronPort-PHdr: 9a23:Kv6FPBeGkPRIv94I5uF8BLAnlGMj4u6mDksu8pMizoh2WeGdxc6+YB7h7PlgxGXEQZ/co6odzbGG7+a7BSdasN6oizMrTt9lb1c9k8IYnggtUoauKHbQC7rUVRE8B9lIT1R//nu2YgB/Ecf6YEDO8DXptWZBUiv2OQc9HOnpAIma153xjLDuvcaPKF8ZzBOGIppMbzyO5T3LsccXhYYwYo0Q8TDu5kVyRuJN2GlzLkiSlRuvru25/Zpk7jgC86l5r50IAu3GePEzRLlcSTAnKHwd5cvxtBCFQxHcyGEbVzAzmwBUAg6Nzhj/VJD3+n//v/Bh2S/cP8TzR7cycTmk9OJvWRjuziAdOGhqoynslsVsgfcD81qarBtlztuMbQ== Authentication-Results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=mjambon@gmail.com; spf=Pass smtp.mailfrom=mjambon@gmail.com; spf=None smtp.helo=postmaster@mail-pa0-f41.google.com Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of mjambon@gmail.com) identity=pra; client-ip=209.85.220.41; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="mjambon@gmail.com"; x-sender="mjambon@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of mjambon@gmail.com designates 209.85.220.41 as permitted sender) identity=mailfrom; client-ip=209.85.220.41; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="mjambon@gmail.com"; x-sender="mjambon@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-pa0-f41.google.com) identity=helo; client-ip=209.85.220.41; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="mjambon@gmail.com"; x-sender="postmaster@mail-pa0-f41.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0DuAQADdvxVlCncVdFdgneBacZxPBABAQEBAQEBARABAQEBBwsLCR8wgh2CHxURCAE3AgMNBSYWGAMCAQIBIgEFAS8IAQEXB4d3AxIBBKdiggWBMD4xkCuKKScNhQwBBQ6GZYoohBUFjXKHcXeVAJBdNYEXOGWBEAELATcfgXRRiW0BAQE X-IPAS-Result: A0DuAQADdvxVlCncVdFdgneBacZxPBABAQEBAQEBARABAQEBBwsLCR8wgh2CHxURCAE3AgMNBSYWGAMCAQIBIgEFAS8IAQEXB4d3AxIBBKdiggWBMD4xkCuKKScNhQwBBQ6GZYoohBUFjXKHcXeVAJBdNYEXOGWBEAELATcfgXRRiW0BAQE X-IronPort-AV: E=Sophos;i="5.17,554,1437429600"; d="scan'208";a="178303618" Received: from mail-pa0-f41.google.com ([209.85.220.41]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 18 Sep 2015 22:38:57 +0200 Received: by pacex6 with SMTP id ex6so60003896pac.0 for ; Fri, 18 Sep 2015 13:38:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:message-id:date:from:user-agent:mime-version:to:subject :content-type:content-transfer-encoding; bh=Ox4BXozGh1cw3AJobl2QTsu2j3SStaahUK/N8FYM/3A=; b=c79V8ChJRSPrqKaRXU27ykb6Jjv93vYnT62+6mtA/WNFg4JmY382IK5yj/Frz3ADyU zKWjm2JhTdCJNJpWAeTRdUgUIZ2FJ9VOzZ80+ccp3+ra4Mq+MtjkT/h5oOhhByuaBQl/ b8xky/y68Kn1MOzXt7y40guuDe53C0ue9hjxRT2pmvFGMQ98Gnntno+HpMwyYno3zWZq /nI1gtxWaf3xf17zS7WQ9bGOfp/BL/vTl33o0fqXqqK5AD9TO5OS/kLStDMNsWn9S/Gv VzrDNX8BXJH7MYN/bfGC2GE7PpvVxDXX60TnXfN4o1WsOWa+Xi1hB0kxnzxtSv1K3crv zuag== X-Received: by 10.69.12.33 with SMTP id en1mr9171360pbd.97.1442608735409; Fri, 18 Sep 2015 13:38:55 -0700 (PDT) Received: from ?IPv6:2601:646:c201:9a00:c9b9:14cc:5c13:80c? ([2601:646:c201:9a00:c9b9:14cc:5c13:80c]) by smtp.googlemail.com with ESMTPSA id bh5sm10622021pbc.5.2015.09.18.13.38.54 for (version=TLSv1/SSLv3 cipher=OTHER); Fri, 18 Sep 2015 13:38:54 -0700 (PDT) Sender: Martin Jambon Message-ID: <55FC765D.4020005@ens-lyon.org> Date: Fri, 18 Sep 2015 13:38:53 -0700 From: Martin Jambon User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: caml-list@inria.fr Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: [Caml-list] Polymorphic arguments Is there a way to make this work? let get_a resource = 123 let get_b resource = "abc" let get_a_b with_resource = (with_resource get_a, with_resource get_b) ^^^^^ Error: This expression has type 'a -> bytes but an expression was expected of type 'a -> int Type bytes is not compatible with type int ... such that the function get_a_b is simply called by passing a with_resource function as argument without exotic syntax or annotation required from the user, e.g. let with_resource f = ... let a, b = get_a_b with_resource I know the solution with a polymorphic record, but it is cumbersome to use since the with_resource function has to be wrapped in a record. I am unclear on what is possible to do without a record. The following works and I'd like to avoid that: type resource type with_resource = { with_resource: 'a. (resource -> 'a) -> 'a; } let get_a resource = 123 let get_b resource = "abc" let get_a_b {with_resource} = (with_resource get_a, with_resource get_b) Note that in practice this is used with computations that take place at different times and one of the goals is to avoid hanging on to the resource for too long, so the following is not acceptable: let get_a_b with_resource = with_resource (fun resource -> get_a resource; sleep 60; get_b resource ) With lwt, a sample with_resource function would be the following with_connection: val with_connection : (connection -> 'a Lwt.t) -> 'a Lwt.t let with_connection f = get_connection () >>= fun conn -> Lwt.finalize f (fun () -> recycle_connection conn) and the desired get_a_b function would be: let get_a conn : int Lwt.t = ... let get_b conn : string Lwt.t = ... let get_a_b with_connection = with_connection get_a >>= fun a -> Lwt_unix.sleep 60. >>= fun () -> with_connection get_b >>= fun b -> Lwt.return (a, b) Martin