caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Format question
@ 2002-11-06 22:49 Oleg
  2002-11-06 23:46 ` David Brown
  2002-11-20  0:29 ` David Frese
  0 siblings, 2 replies; 6+ messages in thread
From: Oleg @ 2002-11-06 22:49 UTC (permalink / raw)
  To: caml-list

Hi

Is it possible to define a printf that automatically flushes stdout?

let my_printf x = Printf.printf x; flush stdout;;

my_printf "%i" 3;;

doesn't work (Incorrect number of arguments, as the type of my_printf is 
different from Printf.printf)

Oleg
-------------------
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] 6+ messages in thread

* Re: [Caml-list] Format question
  2002-11-06 22:49 [Caml-list] Format question Oleg
@ 2002-11-06 23:46 ` David Brown
  2002-11-20  0:29 ` David Frese
  1 sibling, 0 replies; 6+ messages in thread
From: David Brown @ 2002-11-06 23:46 UTC (permalink / raw)
  To: Oleg; +Cc: caml-list

On Wed, Nov 06, 2002 at 05:49:41PM -0500, Oleg wrote:

> Is it possible to define a printf that automatically flushes stdout?
> 
> let my_printf x = Printf.printf x; flush stdout;;
> 
> my_printf "%i" 3;;

The problem is that printf takes multiple arguments, or at least appears
to.  You could try:

let my_printf x =
  let res = Printf.printf x in
  flush stdout;
  res

which would be correctly typed, but the flush would happen right before
the first format string.

Really, you would have to grab the definition of printf from the source,
and change it to do what you want.  This makes it a lot less portable.

let my_fprintf chan fmt =
  let fmt = (Obj.magic fmt : string) in
  let len = String.length fmt in
  let rec doprn i =
    if i >= len then begin flush chan; Obj.magic () end else
    match String.unsafe_get fmt i with
    | '%' -> Printf.scan_format fmt i cont_s cont_a cont_t
    | c   -> output_char chan c; doprn (succ i)
  and cont_s s i =
    output_string chan s; doprn i
  and cont_a printer arg i =
    printer chan arg; doprn i
  and cont_t printer i =
    printer chan; doprn i
  in doprn 0

let my_printf fmt = my_fprintf stdout fmt
-------------------
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] 6+ messages in thread

* Re: [Caml-list] Format question
  2002-11-06 22:49 [Caml-list] Format question Oleg
  2002-11-06 23:46 ` David Brown
@ 2002-11-20  0:29 ` David Frese
  1 sibling, 0 replies; 6+ messages in thread
From: David Frese @ 2002-11-20  0:29 UTC (permalink / raw)
  To: Oleg; +Cc: Caml-list

On Wed, 2002-11-06 at 23:49, Oleg wrote:
> Hi
> 
> Is it possible to define a printf that automatically flushes stdout?
> 
> let my_printf x = Printf.printf x; flush stdout;;
> 
> my_printf "%i" 3;;
> 
> doesn't work (Incorrect number of arguments, as the type of my_printf is 
> different from Printf.printf)

Hello,

I read your message a little late, but this seems to work:


let flprintf ch fmt =
  let rec test x =
    if (Obj.magic x) = ()
    then begin flush ch; Obj.magic () end
    else Obj.magic (fun next -> test ((Obj.magic x) next))
  in
    test (Printf.fprintf ch fmt)

let my_printf = flprintf stdout


As far as I know, no function will ever be represented as 0, which is
the internal representation of (). The code depends on that fact...

David.

-------------------
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] 6+ messages in thread

* Re: [Caml-list] Format question
  2002-11-07  0:09 Oleg
  2002-11-07  0:31 ` Alessandro Baretta
@ 2002-11-07  0:46 ` Gerd Stolpmann
  1 sibling, 0 replies; 6+ messages in thread
From: Gerd Stolpmann @ 2002-11-07  0:46 UTC (permalink / raw)
  To: Oleg; +Cc: caml-list


Am 2002.11.07 01:09 schrieb(en) Oleg:
> Oleg wrote: 
> 
> >
> > Is it possible to define a printf that automatically flushes stdout?
> >
> 
> I figured it out:
> 
> let my_printf (x : ('a, out_channel, unit) format) = 
>    let tmp = Printf.printf x in flush stdout; tmp;;

No that does not work, output is flushed before anything is printed,
at least if the format string requires an argument. E.g.

# my_printf "%d" 6; prerr_endline "a";;
a
6- : unit = ()

(prerr_endline implies flushing stderr.) In this example, tmp has
type int->unit, and the 6 is applied to tmp after flushing has
been done.

You need kprintf:

let my_printf = Printf.kprintf (fun s -> print_string s; flush stdout; s);;

Unfortunately, my_printf results always in a string. This can't be changed
in O'Caml 3.06, but I have recently seen that there is an improved
kprintf in CVS.

Gerd

------------------------------------------------------------
Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany 
gerd@gerd-stolpmann.de          http://www.gerd-stolpmann.de
------------------------------------------------------------
-------------------
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] 6+ messages in thread

* Re: [Caml-list] Format question
  2002-11-07  0:09 Oleg
@ 2002-11-07  0:31 ` Alessandro Baretta
  2002-11-07  0:46 ` Gerd Stolpmann
  1 sibling, 0 replies; 6+ messages in thread
From: Alessandro Baretta @ 2002-11-07  0:31 UTC (permalink / raw)
  To: ocaml



Oleg wrote:
> Oleg wrote: 
> 
> 
>>Is it possible to define a printf that automatically flushes stdout?
>>
> 
> 
> I figured it out:
> 
> let my_printf (x : ('a, out_channel, unit) format) = 
>    let tmp = Printf.printf x in flush stdout; tmp;;
> 
> Oleg

I don't think this does what you want it to. This functions 
starts to parse the format string and prints it until the 
first conversion specifier, where it stops. Then it flushes 
stdout and returns a closure which will perform the 
specified conversion. When the function is then invoked on 
the actual parameters of the conversion, stdout is no longer 
flushed.

Basically, this works only if what you're printing is just 
constant strings. But then you're probably better off with

let my_printf x = print_string x; flush stdout

Alex

-------------------
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] 6+ messages in thread

* Re: [Caml-list] Format question
@ 2002-11-07  0:09 Oleg
  2002-11-07  0:31 ` Alessandro Baretta
  2002-11-07  0:46 ` Gerd Stolpmann
  0 siblings, 2 replies; 6+ messages in thread
From: Oleg @ 2002-11-07  0:09 UTC (permalink / raw)
  To: caml-list

Oleg wrote: 

>
> Is it possible to define a printf that automatically flushes stdout?
>

I figured it out:

let my_printf (x : ('a, out_channel, unit) format) = 
   let tmp = Printf.printf x in flush stdout; tmp;;

Oleg
-------------------
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] 6+ messages in thread

end of thread, other threads:[~2002-11-20  0:27 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-11-06 22:49 [Caml-list] Format question Oleg
2002-11-06 23:46 ` David Brown
2002-11-20  0:29 ` David Frese
2002-11-07  0:09 Oleg
2002-11-07  0:31 ` Alessandro Baretta
2002-11-07  0:46 ` Gerd Stolpmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).