List Info

Thread: "ocaml_beginners"::[] Printf: takes "format", does nothing.




"ocaml_beginners"::[] Printf: takes "format", does nothing.
country flaguser name
Moldova, Republic of
2007-02-05 07:24:25

Shalom.

I want to make a function that takes argument of type
"('a, 'b, 'c) format" (or "... format4"), all following
arguments, and silently ignores them all, returning unit
value.
Simple approach doesn't work:

========================================
value (nothingf : format 'a 'b 'c -> unit) _fmt = ()
;

value () = nothingf "%i %c" 123 'a'
;
========================================

$ ocamlopt -w A -pp camlp4r tst.ml -o tst.exe
File "tst.ml", line 4, characters 11-19:
This function is applied to too many arguments,
maybe you forgot a `;'
mingw32-make.exe: *** [tst.exe] Error 2

Why I need it? I want to create a function that prints
debug messages, so that the function was either
(Printf.fprintf logfile "myformat" myargs) or
(nothingf "myformat" myargs) depending on some boolean
value (for example, "do_write_debug_info : bool").

Now I have the following code (application is
windows-specific, for unix there will be "/dev/null"
instead of "NUL"):

value do_write_debug_info = False
;

value debug_info_filename =
if do_write_debug_info
then "debug.log"
else "NUL"
;

value debug_info_channel = open_out debug_info_filename
;

value (dbg : format 'a out_channel unit -> 'a) fmt =
Printf.fprintf debug_info_channel fmt
;

value () = dbg "%i %c" 123 'a'
;

But I think that it's a bit ugly and sometimes slow to
make all formatting just for sending it to /dev/null.

--
WBR,
dmitry mailto: gds-mlsts%40moldavcable.com">gds-mlstsmoldavcable.com

__._,_.___
.

__,_._,___
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )