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: Jean-Christophe Filliatre <Jean-Christophe.Filliatre@l...>
Subject: Re: [Caml-list] kprintf with user formatters

Pierre Weis writes:
 > 
 > If I understand properly:
 > 
 > - you want to skip the runtime time penalty of formatting the
 > arguments to string before discarding the result,
 > - you even want not to parse the format string,
 > - ideally you also want NOT TO EVALUATE the reminding arguments of
 > your printf call ?
 > 
 > Hmm, this sounds extremely lazy to me; so this suggests thunk
 > programming; hey, we have that in the language, so let's go!
 > 
 > let log level thunk =
 >  if may_log level then thunk ();;
 > 
 > ...
 > 
 >  log 2 (fun () ->
 >    eprintf "Argument 1 is hard to compute %d\n" (ackermann x x))

I  also  use  the same  kind  of  trick  in  practice, with  a  little
refinement  to avoid  building the  closure. I  introduce higher-order
functions such as

   val if_debug : ('a -> unit) -> 'a -> unit

which behaves  like application  when the debug  flag is on  (and does
nothing otherwise). Then you can simply write

   ...
   if_debug eprintf "this is a message";
   ...

I even introduce variants for functions with more than one argument to
be able to write stuff like

   ...
   if_debug3 eprintf "syntax tree is %a@." print_tree t;
   ...

without addition of parentheses. I still find this very convenient and
not obtrusive as far as style is concerned.

Hope this helps,
-- 
Jean-Christophe Filliâtre (http://www.lri.fr/~filliatr)

-------------------
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