<div dir="ltr">I've uploaded this change (with a much more useful error message) as D327 on Phabricator.  I have done some very limited testing, and it passes GHC's validation (on Linux), but I would really appreciate if some people who use a lot of lazy IO could test this against their programs to make sure it doesn't produce errors in any cases when it shouldn't.<br><br>Thanks,<br>David<br><div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jul 21, 2014 at 4:16 PM, David Feuer <span dir="ltr"><<a href="mailto:david.feuer@gmail.com" target="_blank">david.feuer@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Currently,<br>
<br>
withFile "foo" hGetContents >>= putStrLn<br>
<br>
prints out an empty line, the better to confuse newbies.<br>
<br>
I propose modifying the lazyRead function in GHC.IO.Handle.Text that<br>
currently reads<br>
<br>
lazyRead :: Handle -> IO String<br>
lazyRead handle =<br>
   unsafeInterleaveIO $<br>
        withHandle "hGetContents" handle $ \ handle_ -> do<br>
        case haType handle_ of<br>
          ClosedHandle     -> return (handle_, "")<br>
          SemiClosedHandle -> lazyReadBuffered handle handle_<br>
          _ -> ioException<br>
                  (IOError (Just handle) IllegalOperation "hGetContents"<br>
                        "illegal handle type" Nothing Nothing)<br>
<br>
to something like<br>
<br>
lazyRead :: Handle -> IO String<br>
lazyRead handle =<br>
   unsafeInterleaveIO $<br>
        withHandle "hGetContents" handle $ \ handle_ -> do<br>
        case haType handle_ of<br>
          ClosedHandle     -> return (handle_, error "Forcing the<br>
result of a lazy read led to an attempt to read from a closed<br>
handle.")<br>
          SemiClosedHandle -> lazyReadBuffered handle handle_<br>
          _ -> ioException<br>
                  (IOError (Just handle) IllegalOperation "hGetContents"<br>
                        "illegal handle type" Nothing Nothing)<br>
<br>
Ideally that error should instead be something to throw an imprecise<br>
exception, but I don't know how to use those yet. I can't personally<br>
see a way for this to break sane, working code, but the folks on #ghc<br>
thought it should be discussed and debated on list.<br>
<span class=""><font color="#888888"><br>
David Feuer<br>
</font></span></blockquote></div><br></div></div></div>