[MLton] Bug: TextPrimIO does not distinguish between EOF and no data

Ville Laurikari ville at laurikari.net
Tue Dec 11 04:17:41 PST 2007


Hi,

I noticed bad behavior with TextIO.canInput.  If the input stream has
no data to read currently but is not yet at EOF, canInput returns
"SOME 0" instead of NONE.  Also, the eof condition is set for the
stream and all further read attempts fail as if the stream was at EOF.

Attached is a program which demonstrates the problem.

  $ mlton bug.sml
  $ (echo foo; sleep 1; echo bar; sleep 1) | ./bug
  foo
  EOF

Expected output would probably be more like this:
  foo
  Would block.
  bar
  Would block.
  EOF

I dug around a bit, and found out that it seems that a readArrNB made
with PrimIO (in basis-library/io/prim-io.fun) never returns NONE (to
signify no data), but always returns SOME _.  In case of no data, we
get SOME 0 which means EOF.  This readArrNB is implemented as
   SOME (SOME o (readArr "readVecNB"))
Am I just confused or is this the problem?  If the primitive reader
does not distinguish between EOF and no data, TextIO surely cannot
either.

--
http://www.iki.fi/vl/
-------------- next part --------------
fun loop stream =
    case TextIO.canInput (stream, 1) of
       NONE => (print "Would block.\n"
	      ; OS.Process.sleep (Time.fromSeconds 1)
	      ; loop stream)
     | SOME _ =>
       case TextIO.inputLine stream of
	  SOME line => (print line
                      ; loop stream)
	| NONE => print "EOF\n"
val () = loop TextIO.stdIn


More information about the MLton mailing list