+1 from me personally. Even as a "breaking change" it has semantics that are much more sane. <div><br></div><div>Any code that can see the exception has a deeply flawed model of how withFile works and has behavior that will vary fairly wildly across platforms, give silently wrong answers, etc.</div>
<div><br></div><div>This is just my personal stamp of approval, though, not necessarily the informed opinion of the core libraries committee.<br><br>On Tuesday, July 22, 2014, Petr Pudlák <<a href="mailto:petr.mvd@gmail.com">petr.mvd@gmail.com</a>> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><p dir="ltr">+1</p>
<p dir="ltr">Petr</p>
<div class="gmail_quote">Dne 21. 7. 2014 22:16 "David Feuer" <<a href="javascript:_e(%7B%7D,'cvml','david.feuer@gmail.com');" target="_blank">david.feuer@gmail.com</a>> napsal(a):<br type="attribution">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;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>
<br>
David Feuer<br>
_______________________________________________<br>
Libraries mailing list<br>
<a href="javascript:_e(%7B%7D,'cvml','Libraries@haskell.org');" target="_blank">Libraries@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/libraries" target="_blank">http://www.haskell.org/mailman/listinfo/libraries</a><br>
</blockquote></div>
</blockquote></div>