Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: 2004-06-30 (16:34)
From: Damien <Damien.Pous@e...>
Subject: [Caml-list] kprintf with user formatters

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 = 
    (fun s -> Mutex.lock m; print_endline s; Mutex.unlock m) 
module Debug :  sig 
  val m: Mutex.t
  val printf : ('a, unit, string, unit) format4 -> 'a 

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;
(* 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)


To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: