From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.3 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 55CBFBBC1 for ; Thu, 24 Apr 2008 10:39:18 +0200 (CEST) X-IronPort-AV: E=Sophos;i="4.25,703,1199660400"; d="asc'?scan'208";a="11865586" Received: from peray.inria.fr (HELO ausone.inria.fr) ([128.93.8.98]) by mail3-relais-sop.national.inria.fr with SMTP; 24 Apr 2008 10:39:18 +0200 Received: by ausone.inria.fr (sSMTP sendmail emulation); Thu, _d Apr 2008 10:39:09 +0200 From: "Nicolas Pouillard" Cc: Caml_mailing list Subject: Re: [Caml-list] ifprint and format type question To: Brian Hurt References: <480F9F45.4040101@janestcapital.com> In-Reply-To: <480F9F45.4040101@janestcapital.com> Date: Thu, 24 Apr 2008 10:39:04 +0200 Message-Id: <1209026061-sup-6115@ausone.inria.fr> User-Agent: Sup/0.5 Content-Type: multipart/signed; protocol="application/pgp-signature"; boundary="=-1209026349-117095-38737-9637-1-="; micalg="pgp-sha1" MIME-Version: 1.0 X-Spam: no; 0.00; foo:01 printf:01 printf:01 fprintf:01 foo:01 fprintf:01 unify:01 fmt:01 fmt:01 caml-list:01 strings:01 strings:01 output:02 output:02 string:02 X-Attachments: cset="UTF-8" type="application/pgp-signature" name="signature.asc" name="signature.asc" --=-1209026349-117095-38737-9637-1-= Content-Type: text/plain; charset=UTF-8 Excerpts from Brian Hurt's message of Wed Apr 23 22:42:45 +0200 2008: > So, I'm trying to write code like (simplifying): > > let my_output (_: string) = ();; (* the real code is much more > complicated but not relevant *) > > let foo b fmt = > if not b then > Printf.ifprintf () fmt > else > Printf.ksprintf my_output fmt > ;; ifprintf works well with fprintf let foo b fmt = if not b then Printf.ifprintf oc fmt else Printf.fprintf oc fmt ;; Otherwise using Format.ifprintf could help due to its generalized notion of formatter. > The problem is that the above code doesn't compile- ifprintf wants fmt > to be ('b, unit, unit) format = ('b, unit, unit, unit) format4, while > ksprintf wants it to be ('b, unit, string, 'a) format4. Now, I could do > the above like: > > let foo b fmt = > Printf.ksprintf (fun s -> if b then my_output s) fmt > > but the point and purpose of using ifprintf is to avoid the cost of > converting the arguments to strings that are just going to be thrown away. Yes this defeats the purpose. > So, my questions are: > > 1: is there a way to make this work without using Obj.magic or > rewritting isprintf? With Printf.ksprintf I would say no. > 2: is there a reason ifprintf has the type 'a -> ('b, 'a, unit) format > -> 'b, instead of ('b, 'a, 'c) format -> 'b, or better yet ('b, 'a, 'c, > 'd) format4 -> 'b, or even better yet ('b, 'a, 'c, 'd, 'e, 'f) format6 > -> 'b (allowing it to unify with more different formats)? Hum there perhaps room for a more general ifprintf. > 3: Does ifprintf actually avoid the cost of converting it's arguments to > strings? The code is unclear. If the answer to this is 'no', the other > two questions are moot. Yes it does avoid the cost of converting it's arguments. -- Nicolas Pouillard aka Ertai --=-1209026349-117095-38737-9637-1-= Content-Disposition: attachment; filename="signature.asc" Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.8 (Darwin) iEYEARECAAYFAkgQRygACgkQj+FCNw9dwLlT7wCfafJX7Sc4KP2toBojfI5x8FS5 ZZkAn3OFIBqLzrFslewvTBAB2XORQZ01 =l/bI -----END PGP SIGNATURE----- --=-1209026349-117095-38737-9637-1-=--