Proposal: end lazy IO results with errors/exceptions

Edward Kmett ekmett at gmail.com
Thu Jul 24 20:16:43 UTC 2014


+1 from me personally. Even as a "breaking change" it has semantics that
are much more sane.

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.

This is just my personal stamp of approval, though, not necessarily the
informed opinion of the core libraries committee.

On Tuesday, July 22, 2014, Petr Pudlák <petr.mvd at gmail.com> wrote:

> +1
>
> Petr
> Dne 21. 7. 2014 22:16 "David Feuer" <david.feuer at gmail.com
> <javascript:_e(%7B%7D,'cvml','david.feuer at gmail.com');>> napsal(a):
>
>> Currently,
>>
>> withFile "foo" hGetContents >>= putStrLn
>>
>> prints out an empty line, the better to confuse newbies.
>>
>> I propose modifying the lazyRead function in GHC.IO.Handle.Text that
>> currently reads
>>
>> lazyRead :: Handle -> IO String
>> lazyRead handle =
>>    unsafeInterleaveIO $
>>         withHandle "hGetContents" handle $ \ handle_ -> do
>>         case haType handle_ of
>>           ClosedHandle     -> return (handle_, "")
>>           SemiClosedHandle -> lazyReadBuffered handle handle_
>>           _ -> ioException
>>                   (IOError (Just handle) IllegalOperation "hGetContents"
>>                         "illegal handle type" Nothing Nothing)
>>
>> to something like
>>
>> lazyRead :: Handle -> IO String
>> lazyRead handle =
>>    unsafeInterleaveIO $
>>         withHandle "hGetContents" handle $ \ handle_ -> do
>>         case haType handle_ of
>>           ClosedHandle     -> return (handle_, error "Forcing the
>> result of a lazy read led to an attempt to read from a closed
>> handle.")
>>           SemiClosedHandle -> lazyReadBuffered handle handle_
>>           _ -> ioException
>>                   (IOError (Just handle) IllegalOperation "hGetContents"
>>                         "illegal handle type" Nothing Nothing)
>>
>> Ideally that error should instead be something to throw an imprecise
>> exception, but I don't know how to use those yet. I can't personally
>> see a way for this to break sane, working code, but the folks on #ghc
>> thought it should be discussed and debated on list.
>>
>> David Feuer
>> _______________________________________________
>> Libraries mailing list
>> Libraries at haskell.org
>> <javascript:_e(%7B%7D,'cvml','Libraries at haskell.org');>
>> http://www.haskell.org/mailman/listinfo/libraries
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/libraries/attachments/20140724/0211e807/attachment.html>


More information about the Libraries mailing list