Version française
Home     About     Download     Resources     Contact us    
Browse thread
ifprint and format type question
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Brian Hurt <bhurt@j...>
Subject: ifprint and format type question
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
;;

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.

So, my questions are:

1: is there a way to make this work without using Obj.magic or 
rewritting isprintf?

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)?

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.

Brian