Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Caml-list] ifprint and format type question

15 views
Skip to first unread message

Brian Hurt

unread,
Apr 23, 2008, 4:42:56 PM4/23/08
to caml...@yquem.inria.fr
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

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Nicolas Pouillard

unread,
Apr 24, 2008, 4:39:26 AM4/24/08
to Brian Hurt, Caml_mailing list
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

signature.asc

jan....@gmail.com

unread,
Apr 22, 2013, 3:04:10 PM4/22/13
to
Hi,

I was trying to do the same thing as Brian, has anything changed in this regard? I looked for a way to solve this problem with the functions provided in the standard library but couldn't find what I needed.

I think a function, perhaps named kiprintf, with as signature (unit -> 'a) -> ('b, unit, string, 'a) format4 -> b would be needed for this.
The best place to implement it would probably be the printf module in stdlib, as implementing it would need some internal stuff from that module.
If there is any interest I'm willing to implement this myself and provide the code.
(For now I have solved my problem with a camlp4 macro, so I don't immediately need it for myself, but I might want it again in the future.)

Kind regards,

Jan Doms
0 new messages