caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Nicolas Pouillard" <nicolas.pouillard@gmail.com>
To: Brian Hurt <bhurt@janestcapital.com>
Cc: Caml_mailing list <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] ifprint and format type question
Date: Thu, 24 Apr 2008 10:39:04 +0200	[thread overview]
Message-ID: <1209026061-sup-6115@ausone.inria.fr> (raw)
In-Reply-To: <480F9F45.4040101@janestcapital.com>

[-- Attachment #1: Type: text/plain, Size: 1844 bytes --]

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

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 194 bytes --]

      reply	other threads:[~2008-04-24  8:39 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-04-23 20:42 Brian Hurt
2008-04-24  8:39 ` Nicolas Pouillard [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1209026061-sup-6115@ausone.inria.fr \
    --to=nicolas.pouillard@gmail.com \
    --cc=bhurt@janestcapital.com \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).