
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.