[Haskell-cafe] cannot catch exception
Viktor Dukhovni
ietf-dane at dukhovni.org
Mon Mar 4 22:02:26 UTC 2019
On Mon, Mar 04, 2019 at 09:26:41PM +0100, Kees Bleijenberg wrote:
> 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
Take a close look at the three increasingly strict examples below:
ghci> do { x <- return $ Right $ undefined ; putStrLn "delayed..."; case x of { Right _ -> return 1; _ -> return 0 } }
delayed...
1
ghci> do { x <- return $ Right $! undefined; putStrLn "delayed..."; case x of { Right _ -> return 1; _ -> return 0 } }
delayed...
*** Exception: Prelude.undefined
CallStack (from HasCallStack):
error, called at libraries/base/GHC/Err.hs:78:14 in base:GHC.Err
undefined, called at <interactive>:8:29 in interactive:Ghci3
ghci> do { x <- return $! Right $! undefined ; putStrLn "delayed..."; case x of { Right _ -> return 1; _ -> return 0 } }
*** Exception: Prelude.undefined
CallStack (from HasCallStack):
error, called at libraries/base/GHC/Err.hs:78:14 in base:GHC.Err
undefined, called at <interactive>:9:30 in interactive:Ghci3
While "Right $! x" is strict in x, "return $ Right $! x" is not!
You need "return $! Right $! x" to make that happen.
--
Viktor.
More information about the Haskell-Cafe
mailing list