From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL,HTML_MESSAGE autolearn=disabled version=3.1.3 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 34B4DBC6B for ; Tue, 6 Nov 2007 19:49:20 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAACdGMEfAXQInh2dsb2JhbACCPjWMDAEICik X-IronPort-AV: E=Sophos;i="4.21,379,1188770400"; d="scan'208,217";a="4119595" Received: from concorde.inria.fr ([192.93.2.39]) by mail1-smtp-roc.national.inria.fr with ESMTP; 06 Nov 2007 19:49:20 +0100 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id lA6InJ8r026141 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Tue, 6 Nov 2007 19:49:19 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAACdGMEdCm3xrnWdsb2JhbACCPjWMDAEBCw8I X-IronPort-AV: E=Sophos;i="4.21,379,1188770400"; d="scan'208,217";a="19006081" Received: from www.janestcapital.com (HELO smtp.janestcapital.com) ([66.155.124.107]) by mail4-smtp-sop.national.inria.fr with ESMTP; 06 Nov 2007 19:49:18 +0100 Received: from [172.25.129.161] [38.96.172.125] by janestcapital.com with ESMTP (SMTPD-9.10) id A72C0874; Tue, 06 Nov 2007 13:49:16 -0500 Message-ID: <4730B72B.3000109@janestcapital.com> Date: Tue, 06 Nov 2007 13:49:15 -0500 From: Brian Hurt User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Till Varoquaux Cc: Nicolas Pouillard , tmp123 , caml-list Subject: Re: [Caml-list] log function without evaluate arguments References: <47309EEC.4080706@menta.net> <1194373338-sup-9994@ausone.local> <9d3ec8300711061040l424aac69y4b859a029d815089@mail.gmail.com> In-Reply-To: <9d3ec8300711061040l424aac69y4b859a029d815089@mail.gmail.com> Content-Type: multipart/alternative; boundary="------------060900070206030904040201" X-Miltered: at concorde with ID 4730B72F.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; printf:01 printf:01 val:01 non-trivial:01 ocaml:01 val:01 non-trivial:01 ocaml:01 imho:01 imho:01 wrote:01 wrote:01 caml-list:01 computation:01 computation:01 X-Attachments: cset="UTF-8" cset="UTF-8" This is a multi-part message in MIME format. --------------060900070206030904040201 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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). > >Till > > 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. Brian --------------060900070206030904040201 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit 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).

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

Brian


--------------060900070206030904040201--