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: Sylvain Le Gall <sylvain@l...>
Subject: Re: log function without evaluate arguments
On 06-11-2007, tmp123 <tmp123@menta.net> wrote:
> 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.
>
> If my understanting of the reading has been correct, one of the best
> options seems to be something like (in camlp4r revised syntax, I'm more
> used to it):
>
> value log_flag = ref False;
>
> value log e =
>   if log_flag.val
>   then
>     Lazy.force e
>   else ();
>
> and the callers must include lines like:
>
> log (lazy (Printf.printf "%d" (sum 1 3)));
>
> Please, is this a good way? Some sugestions to made it better in
> performance or syntax?
>
> Thanks a lot.
>

I am used to :
 value log e =
   if log_flag.val then
     e ()
   else 
     ();

avec log (fun () -> (Printf.printf "%d" (sum 1 3)))

But the enhancement is minor.

Regards,
Sylvain Le Gall