English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

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: 2007-11-06 (18:49)
From: Brian Hurt <bhurt@j...>
Subject: Re: [Caml-list] log function without evaluate arguments
Till Varoquaux wrote:

>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).
IMHO: evaluating the arguments of your log statement will avoid some 
really ugly heisenbugs- what if awfully_long_computation performs I/O or 
otherwise has side effects?.  Note that having side-effects in your 
arguments to the log statements is a really bad idea, but people will do 
it, and finding where they do it is non-trivial.

Also, creating a lazy thunk in Ocaml is expensive (like 140+ clock 
cycles), while passing an argument into a function is cheap- and the 
common case will be that the argument won't need to be evaluated, just 
passed in.

Translation: don't try to be too clever to avoid evaluating arguments.