Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Printf question
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: William Lovas <wlovas@s...>
Subject: Re: [Caml-list] Printf question
On Sat, May 17, 2003 at 08:34:27PM -0500, Brian Hurt wrote:
> let debug s = Printf.printf ("FOO: " ^ s ^ "\n")
> 
> and:
> 
> let debug s = let t = "FOO: " ^ s ^ "\n" in Printf.printf s
> 
> and neither works.

This is due to the funny type magic that makes printf work in the first
place.

>     debug "some message"; (* outputs "FOO: some message\n" *)
>     debug "answer: %d" 42 ; (* outputs "FOO: answer: 42\n" *)
> 
> basically to prepend "FOO: " and append "\n".  Is this possible in any 
> sane manner?  I tried:

I think the right solution (or at least, *a* right solution) in this case
is to use kprintf, which takes a continuation to pass the resulting string
to.  For an sprintf-like solution:

    let debug s = Printf.kprintf (fun t -> "FOO: " ^ t ^ "\n") s

Or, for something more side-effectual:

    let debug s = Printf.kprintf
                    (fun t -> print_string ("FOO: " ^ t ^ "\n"); "") s

(But you might have to use `ignore' to ignore the useless empty string
result value.)

cheers,
William

-------------------
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