* [Caml-list] selective printf-like function
@ 2002-11-21 1:21 Andy Chou
2002-11-21 17:17 ` Chris Hecker
0 siblings, 1 reply; 3+ messages in thread
From: Andy Chou @ 2002-11-21 1:21 UTC (permalink / raw)
To: caml-list
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,
my_printf "%d" 0
but it should only print if a certain condition is true, which it
evaluates first.
I haven't had any luck yet, and I was wondering if any Caml gurus could
help.
-Andy
-------------------
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] selective printf-like function
2002-11-21 1:21 [Caml-list] selective printf-like function Andy Chou
@ 2002-11-21 17:17 ` Chris Hecker
2002-11-22 9:41 ` Pierre Weis
0 siblings, 1 reply; 3+ messages in thread
From: Chris Hecker @ 2002-11-21 17:17 UTC (permalink / raw)
To: acc, caml-list
>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
-------------------
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] selective printf-like function
2002-11-21 17:17 ` Chris Hecker
@ 2002-11-22 9:41 ` Pierre Weis
0 siblings, 0 replies; 3+ messages in thread
From: Pierre Weis @ 2002-11-22 9:41 UTC (permalink / raw)
To: Chris Hecker; +Cc: acc, caml-list
> >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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2002-11-22 9:41 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-11-21 1:21 [Caml-list] selective printf-like function Andy Chou
2002-11-21 17:17 ` Chris Hecker
2002-11-22 9:41 ` Pierre Weis
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox