* printf "%a" vs sprintf "%a"
@ 2009-03-27 8:53 Bertrand Jeannet
0 siblings, 0 replies; 2+ messages in thread
From: Bertrand Jeannet @ 2009-03-27 8:53 UTC (permalink / raw)
To: caml-list
For people that do not use OCaml batteries, a sprintf function similar
to (Printf|Format).printf can be defined using kfprintf:
let sprintf format =
let buffer = Buffer.create 512 in
let fmt = Format.formatter_of_buffer buffer in
Format.kfprintf
(begin fun fmt ->
Format.pp_print_flush fmt ();
let s = Buffer.contents buffer in
Buffer.clear buffer;
s
end)
fmt
format
The types are different:
Format.fprintf : Format.formatter -> ('a, Format.formatter, unit, unit)
format4 -> 'a
val sprintf : ('a, Format.formatter, unit, string) format4 -> 'a = <fun>
but that is normal, as sprintf should return a string.
Bertrand Jeannet
^ permalink raw reply [flat|nested] 2+ messages in thread
* printf "%a" vs sprintf "%a"
@ 2009-03-26 16:53 Tiphaine Turpin
0 siblings, 0 replies; 2+ messages in thread
From: Tiphaine Turpin @ 2009-03-26 16:53 UTC (permalink / raw)
To: caml-list
Hi,
I'am confused by the interpretation of "%a":
# Printf.printf "%a";;
- : (out_channel -> '_a -> unit) -> '_a -> unit = <fun>
# Printf.sprintf "%a";;
- : (unit -> '_a -> string) -> '_a -> string = <fun>
Usually, the typing of formatting functions is such that
printf something
has type unit if and only if
sprintf something
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 ?
Tiphaine
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2009-03-27 8:59 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-03-27 8:53 printf "%a" vs sprintf "%a" Bertrand Jeannet
-- strict thread matches above, loose matches on Subject: below --
2009-03-26 16:53 Tiphaine Turpin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox