From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id JAA03149; Fri, 16 Jul 2004 09:13:23 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id JAA04433; Fri, 16 Jul 2004 09:13:22 +0200 (MET DST) Received: from lri.lri.fr (lri.lri.fr [129.175.15.1]) by nez-perce.inria.fr (8.12.10/8.12.10) with ESMTP id i6G7DKEV006781; Fri, 16 Jul 2004 09:13:21 +0200 Received: from localhost (localhost [127.0.0.1]) by lri.lri.fr (Postfix) with ESMTP id CB1D319E976; Fri, 16 Jul 2004 09:13:20 +0200 (CEST) Received: from lri.lri.fr ([127.0.0.1]) by localhost (lri.lri.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 04993-03; Fri, 16 Jul 2004 09:13:20 +0200 (CEST) Received: from pc8-123 (pc8-123 [129.175.8.123]) by lri.lri.fr (Postfix) with ESMTP id ADE7419E972; Fri, 16 Jul 2004 09:13:20 +0200 (CEST) Received: from filliatr by pc8-123 with local (Exim 3.35 #1 (Debian)) id 1BlMuG-0004GU-00; Fri, 16 Jul 2004 09:13:20 +0200 From: Jean-Christophe Filliatre MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Message-ID: <16631.32784.526557.747099@gargle.gargle.HOWL> Date: Fri, 16 Jul 2004 09:13:20 +0200 To: Pierre Weis Cc: basile.starynkevitch@inria.fr (Basile Starynkevitch [local]), caml-list@inria.fr Subject: Re: [Caml-list] kprintf with user formatters In-Reply-To: <200407160647.IAA02290@pauillac.inria.fr> References: <20040715142214.GB28587@bourg.inria.fr> <200407160647.IAA02290@pauillac.inria.fr> X-Mailer: VM 7.03 under Emacs 20.7.2 Reply-To: Jean-Christophe.Filliatre@lri.fr (Jean-Christophe Filliatre) X-Virus-Scanned: by amavisd-new at lri.fr X-Miltered: at nez-perce with ID 40F78010.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; filliatre:01 filliatre:01 lri:01 caml-list:01 kprintf:01 formatters:01 pierre:01 weis:01 runtime:01 discarding:01 ideally:01 printf:01 thunk:01 thunk:01 eprintf:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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