From: Pierre Weis <pierre.weis@inria.fr>
To: checker@d6.com (Chris Hecker)
Cc: acc@CS.Stanford.EDU, caml-list@inria.fr
Subject: Re: [Caml-list] selective printf-like function
Date: Fri, 22 Nov 2002 10:41:14 +0100 (MET) [thread overview]
Message-ID: <200211220941.KAA07554@pauillac.inria.fr> (raw)
In-Reply-To: <4.3.2.7.2.20021121091643.033aad28@localhost> from Chris Hecker at "Nov 21, 102 09:17:25 am"
> >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 = <fun>
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
prev parent reply other threads:[~2002-11-22 9:41 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-11-21 1:21 Andy Chou
2002-11-21 17:17 ` Chris Hecker
2002-11-22 9:41 ` Pierre Weis [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=200211220941.KAA07554@pauillac.inria.fr \
--to=pierre.weis@inria.fr \
--cc=acc@CS.Stanford.EDU \
--cc=caml-list@inria.fr \
--cc=checker@d6.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox