Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] kprintf with user formatters
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Damien <Damien.Pous@e...>
Subject: [Caml-list] kprintf with user formatters
Hi,

I am writing a multi-threaded program, so that I need some
synchronization when pretty-printing things :

<<
#directory "+threads";;
#load "unix.cma";;
#load "threads.cma";;
module Debug = struct
  let m = Mutex.create()
  let printf x = 
  Format.kprintf 
    (fun s -> Mutex.lock m; print_endline s; Mutex.unlock m) 
   x
end;;
(*
module Debug :  sig 
  val m: Mutex.t
  val printf : ('a, unit, string, unit) format4 -> 'a 
end
*)
>>

but now, I can't _nicely_ use my previously defined printers :

suppose I have got a type with its toplevel pretty-printer :
<<
type t
val format_t: Format.formatter -> t -> unit
val x: t
>>

I would like to write :
<<
Debug.printf "val t = %a" format_t x
>>

but I cannot since the user printer is expected to be "unit->t->string"
(while it is "formatter->t->unit" for the standard Format.printf "%a")

currently I use :
<<
let (!!) f = (fun () x -> 
  f Format.str_formatter x;
  Format.flush_str_formatter());;
(* val (!!): (Format.formatter -> 'a -> 'b) -> unit -> 'a -> string *)

Debug.printf "val t = %a" !!format_t x
>>

this seems to be working, but I wonder if :
 - there is a better solution
 - this use of the global value "Format.str_formatter" is really
	thread-safe (I would say no...)
 - I missed something
 - printf could not understand a conversion character 'A' that would
	always need a _general_ user-formatter (of type formatter->t->unit)

thanks,
damien

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners