Version française
Home     About     Download     Resources     Contact us    
Browse thread
printf "%a" vs sprintf "%a"
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jérémie Dimino <jeremie@d...>
Subject: Re: [Caml-list] printf "%a" vs sprintf "%a"
Tiphaine Turpin wrote:
> has type string. But %a breaks this rule. Wouldn't it be simpler to have
> two separate directives which accept respectively string printers and
> channel printers, regardless of the outer printing function ?

Note that you can do it with batteries and the new printf.

The default "%a" directive always uses a channel printer:

# Print.printf p"%a";;
- : (unit Batteries.IO.output -> '_a -> unit) -> '_a -> unit = <fun>
# Print.sprintf p"%a";;
- : (unit Batteries.IO.output -> '_a -> unit) -> '_a -> string = <fun>

And you can define a "%foo" directive which always uses a string printer:

# let printer_foo k f x = k (fun out -> String.print out (f x));;
val printer_as : (('a Batteries.IO.output -> unit) -> 'b) -> ('c -> string) -> 'c -> 'b = <fun>
# Print.printf p"%foo";;
- : ('_a -> string) -> '_a -> unit = <fun>
# Print.sprintf p"%foo";;
- : ('_a -> string) -> '_a -> string = <fun>

Cheers,
Jérémie