Version française
Home     About     Download     Resources     Contact us    
Browse thread
Printf issues
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Nicolas Pouillard <nicolas.pouillard@g...>
Subject: Re: [Caml-list] Printf issues
On 3/16/07, Till Varoquaux <till.varoquaux@gmail.com> wrote:
> I having typing issues with sprintf:
>
> # Printf.sprintf "%a";;
> - : (unit -> '_a -> string) -> '_a -> string = <fun>
>
> whereas:
>
> # Printf.printf "%a";;
> - : (out_channel -> '_a -> unit) -> '_a -> unit = <fun>
>
> Am I missing something out here? If not why can't sprintf accept the
> same printers as the other?

That's specified this way. It's why I don't often use sprintf but
rather these one (mainly sbprintf):

module Format_plus :
sig
  val ksbprintf : (string -> 'a) -> ('b, Format.formatter, unit, 'a)
format4 -> 'b
  val sbprintf : ('a, Format.formatter, unit, string) format4 -> 'a
end =
struct
  open Format
  let ksbprintf g fmt =
    let buff = Buffer.create 42 in
    let f = formatter_of_buffer buff in
    kfprintf (fun f -> (pp_print_flush f (); g (Buffer.contents buff))) f fmt

  let sbprintf fmt = ksbprintf (fun x -> x) fmt
end

That way it's more uniform and I can reuse the same printers for
fprintf and sbprintf.

Hope this helps,

-- 
Nicolas Pouillard