caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] kprintf with user formatters
@ 2004-06-30 16:32 Damien
  2004-07-14 21:10 ` Pierre Weis
  0 siblings, 1 reply; 56+ messages in thread
From: Damien @ 2004-06-30 16:32 UTC (permalink / raw)
  To: Ocaml

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


^ permalink raw reply	[flat|nested] 56+ messages in thread

end of thread, other threads:[~2004-07-28 12:31 UTC | newest]

Thread overview: 56+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-06-30 16:32 [Caml-list] kprintf with user formatters Damien
2004-07-14 21:10 ` Pierre Weis
2004-07-15  0:17   ` Markus Mottl
2004-07-15  7:30     ` David MENTRE
2004-07-15  7:59       ` Jean-Christophe Filliatre
2004-07-15 23:35         ` henri dubois-ferriere
2004-07-15  7:39     ` Damien
2004-07-15 12:19       ` Markus Mottl
2004-07-15 12:42         ` Basile Starynkevitch [local]
2004-07-15 13:45           ` Markus Mottl
2004-07-15 14:22             ` Basile Starynkevitch [local]
2004-07-15 14:57               ` Markus Mottl
2004-07-16  6:47               ` Pierre Weis
2004-07-16  7:13                 ` Jean-Christophe Filliatre
2004-07-16  7:23                   ` henri dubois-ferriere
2004-07-16  7:44                     ` Jean-Christophe Filliatre
2004-07-16 17:56                   ` Markus Mottl
2004-07-19  9:17                   ` Pierre Weis
2004-07-19  9:32                     ` Jean-Christophe Filliatre
2004-07-16  7:21                 ` henri dubois-ferriere
2004-07-16 17:44                 ` Markus Mottl
2004-07-19 10:10                   ` Pierre Weis
2004-07-19 10:43                     ` Jon Harrop
2004-07-21 15:52                       ` Pierre Weis
2004-07-21 17:43                         ` lazyness in ocaml (was : [Caml-list] kprintf with user formatters) Daniel Bünzli
2004-07-22 16:28                           ` Pierre Weis
2004-07-22 17:03                             ` William Lovas
2004-07-22 23:00                             ` skaller
2004-07-23  3:32                               ` William Lovas
2004-07-28  7:26                               ` Pierre Weis
2004-07-28  8:06                                 ` skaller
2004-07-28  8:29                                   ` Daniel Bünzli
2004-07-28  9:13                                   ` Pierre Weis
2004-07-28  9:36                                     ` skaller
2004-07-28  9:38                                     ` skaller
2004-07-28 10:17                                 ` Jason Smith
2004-07-28 12:31                                   ` skaller
2004-07-21 20:41                         ` [Caml-list] kprintf with user formatters Jon Harrop
2004-07-22 15:39                           ` Pierre Weis
2004-07-22 22:16                             ` [Caml-list] lazy evaluation: [Was: kprintf with user formatters] skaller
2004-07-22 22:42                             ` [Caml-list] kprintf with user formatters skaller
2004-07-22  8:05                         ` [Caml-list] wait instruction lehalle@miriad
2004-07-22  8:40                           ` Olivier Andrieu
2004-07-22 10:35                             ` lehalle@miriad
2004-07-22 10:33                           ` Vitaly Lugovsky
2004-07-16  6:17             ` [Caml-list] kprintf with user formatters Pierre Weis
2004-07-16 17:14               ` Markus Mottl
2004-07-19 10:00                 ` Pierre Weis
2004-07-16  6:02       ` Pierre Weis
2004-07-16  8:42         ` Damien
2004-07-19  9:00           ` Pierre Weis
2004-07-16 16:52         ` Markus Mottl
2004-07-19  9:28           ` Pierre Weis
2004-07-15 22:20     ` Pierre Weis
2004-07-15 23:01       ` Markus Mottl
2004-07-16 16:17     ` james woodyatt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).