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 KAA07518; Fri, 22 Nov 2002 10:41:50 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id KAA07545 for ; Fri, 22 Nov 2002 10:41:49 +0100 (MET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id gAM9fFX05577; Fri, 22 Nov 2002 10:41:15 +0100 (MET) Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id KAA07554; Fri, 22 Nov 2002 10:41:15 +0100 (MET) From: Pierre Weis Message-Id: <200211220941.KAA07554@pauillac.inria.fr> Subject: Re: [Caml-list] selective printf-like function In-Reply-To: <4.3.2.7.2.20021121091643.033aad28@localhost> from Chris Hecker at "Nov 21, 102 09:17:25 am" To: checker@d6.com (Chris Hecker) Date: Fri, 22 Nov 2002 10:41:14 +0100 (MET) Cc: acc@CS.Stanford.EDU, caml-list@inria.fr X-Mailer: ELM [version 2.4ME+ PL28 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > >I'd like to write a function that will optionally print something. It > >seems simple, but I also want it to be able to take format strings and act > >like printf. So for example, > > There was a thread on a related topic just last week. Search the archives. > > Chris Right, but the answer was using Obj.magic, which is not only useless but also harmful to recommand to a beginner (once more, you must consider that you need a correctness proof to use Obj.magic!). So, here is a simpler solution only using regular functional programming: open Printf;; let debug = ref false;; let dprintf fmt = if !debug then kprintf (fun s -> print_string s; "") fmt else kprintf (fun s -> "") fmt;; val dprintf : ('a, unit, string) format -> 'a = The only problem is that dprintf returns a string instead of returning a unit value as we would like it to do. This is due to the type of the first argument to kprintf (string -> string). It will be improved in the next release of Caml, thanks to the better typing of kprintf (the first argument of kprintf now can have the more general type string -> 'a). In the next release, you thus will prefer to write simply: let dprintf fmt = if !debug then kprintf print_string fmt else kprintf ignore fmt;; Anyhow, the preceding version will still be accepted by the new compiler, so that your code will not be broken. Hope this helps, Pierre Weis INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/ ------------------- 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