The problem is non-strict evaluation aka "laziness". "return" doesn't force evaluation of the pure expression you give to it (actual I/O involving its value generally would, but return just puts a wrapper around it), so its evaluation is forced later outside of your code to catch it, when the value is actually examined.

In extreme cases you end up using deepseq's rnf to force full evaluation, but here it's enough to force evaluation to WHNF with ($!) before handing the result off to return. With a lazy ByteString you might need rnf, since ($!) would only force part of the structure of the list of chunks but you would need to force all of the chunks' contents to trigger the decode exception.

On Mon, Mar 4, 2019 at 3:27 PM Kees Bleijenberg <K.Bleijenberg@lijbrandt.nl> wrote:

David,

 

I tried your first suggestion $!. Nothing changed.

When I tried ‘Right <$> evaluate (TE.decodeUtf8 bufferStrict)’ success. handleException catches the exception.

I don’t understand why. Maybe the documentation for the evaluate function below has to do with it:

There is a subtle difference between evaluate x and return $! x, analogous to the difference between throwIO and throw. If the lazy value x throws an exception, return $! x will fail to return an IO action and will throw an exception instead. evaluate x, on the other hand, always produces an IO action; that action will throw an exception upon execution iff x throws an exception upon evaluation.

I don’t fully understand this, but evaluate works. Thanks!

 

Kees

 

readCDFile :: FilePath -> FilePath -> IO (Either String T.Text)

readCDFile baseDir fn = do

  catch ( do

            buffer <- B.readFile (combine baseDir fn)  --reads strict the whole file

            let bufferStrict = B.toStrict buffer

return $ Right $! TE.decodeUtf8 bufferStrict    -- this doesn’t work  

Right <$> evaluate (TE.decodeUtf8 bufferStrict) –- this does   

liftM Right $ evaluate (TE.decodeUtf8 bufferStrict) – this works too  

         ) exceptionHandler

 

 

Van: David Fox [mailto:dsf@seereason.com]

This fixes it by forcing the evaluation of the decode where it can be caught:

 

return $ Right $! TE.decodeUtf8 bufferStrict

 

or 

 

Right <$> evaluate (TE.decodeUtf8 bufferStrict)

 

<snip>


Virusvrij. www.avast.com
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.


--
brandon s allbery kf8nh
allbery.b@gmail.com