Version française
Home     About     Download     Resources     Contact us    
Browse thread
log function without evaluate arguments
[ 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] log function without evaluate arguments
Excerpts from till.varoquaux's message of Tue Nov 06 19:40:19 +0100 2007:
> On 11/6/07, Nicolas Pouillard <nicolas.pouillard@gmail.com> wrote:
> > Excerpts from tmp123's message of Tue Nov 06 18:05:48 +0100 2007:
> > > Hello,
> > >
> > > In order to implement a function that prints log messages only, by
> > > example, if a boolean flag is true, and does nothing elsewhere, I've
> > > been reading the (long) post sequence "kprintf with user formatters"
> > > (2004 Jun 30).
> > >
> > > In this context, "does nothing" means not convert parameters to text nor
> > > evaluate them.
> >
> > Have a look to the Printf.ifprintf [1] function, it does exactly what you want.
> >
> > Regards,
> >
> Actually it doesn't:
> 
>    log (lazy (Printf.printf "%s" (awfully_long_computation ())))
> 
> when log_val is false (or sylvain's solution, which I prefer), will
> not behave like
> 
>    Printf.ifprinf "%s" (awfully_long_computation ())
> 
> (it won't evaluate its arguments).

Of course but you should use %a:

let log fmt = if debug_mode then Printf.fprintf fmt else Printf.ifprintf fmt;;

let awfully_long_computation' oc () = Printf.fprintf "%s" (awfully_long_computation ());;

log "%a" awfully_long_computation' ()

Regards,

-- 
Nicolas Pouillard aka Ertai