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

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Pierre Weis <Pierre.Weis@i...>
Subject: Re: [Caml-list] Evaluation Order
> Tore Lund wrote:
> 
> >   # let _ =
> >       print_string "before";
> >       print_string "MIDDLE";
> >       print_string "after";;
> >   beforeMIDDLEafter- : unit = ()
> > 
> > but not in this one:
> > 
> >   # let _ =
> >       print_string "before";
> >       Format.print_string "MIDDLE";
> >       print_string "after";;
> >   beforeafterMIDDLE- : unit = ()
> > 
> > What's going on here?  
> 
> 	Buffering I guess. print_string buffers using stdio buffers.
> Format.* has its own buffer, which is flushed into the
> stdout buffer too late.
> 
> 
> -- 
> John (Max) Skaller, mailto:skaller@maxtal.com.au
> 10/1 Toxteth Rd Glebe NSW 2037 Australia voice: 61-2-9660-0850
> checkout Vyper http://Vyper.sourceforge.net
> download Interscript http://Interscript.sourceforge.net
> -------------------
> Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
> To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr

Thank you for pointing out this delicate point.

I added your example and some basic explanation to the FAQ: 

----------------------------------------------------------------------
<H2 ALIGN=LEFT><A NAME="impression_format"> Why some printing material
is mixed up and does not appear in the right order ?</A></H2>

<P>If you use printing functions of the <CODE>format</CODE> module,
you might not mix printing commands from <CODE>format</CODE> with
printing commands from the basic I/O system. In effect, the material
printed by functions from the <CODE>format</CODE> module is delayed
(stored into the pretty-printing queue) in order to find out the
proper line breaking to perform with the material at hand. By contrast
low level output is performed with no more buffering than usual I/O
buffering. So that you can observe the following output differences between
pure low level output:

<PRE>
# print_string "before";
  print_string "MIDDLE";
  print_string "after";;
beforeMIDDLEafter- : unit = ()
</PRE> 

and mixed calls to low and high level printing:

<PRE>
# print_string "before";
  Format.print_string "MIDDLE";
  print_string "after";;
beforeafterMIDDLE- : unit = ()
</PRE>

<P>To avoid this kind of problems you should not mix printing orders
from <CODE>format</CODE> and basic printing commands; that's the reason why
when using functions from the <CODE>format</CODE> module, it is
considered good programming habit to open <CODE>format</CODE> globally
in order to completely mask low level printing functions by the high
level printing functions provided by <CODE>format</CODE>.

<P>N.B: <CODE>format</CODE> printings are automatically flushed after
each evaluation in the interective system. Explicit flushes are
also performed by calling the <CODE>print_newline</CODE> function that
emits a line break and empties the pretty printer queue.
----------------------------------------------------------------------

All constructive criticisms are warmly welcome.

Best regards,

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/


-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr