Idiomatic usage of EitherT and errors
I have written the following code to replace the error message I get from
readFile:
module Main where
import Control.Error
import qualified Data.ByteString.Lazy as BSL
replaceLeftT :: Monad m => String -> EitherT String m a -> EitherT String m a
replaceLeftT message e = EitherT $ do
unwrapped <- runEitherT e
let x = case unwrapped of
Left _ -> Left message
y -> y
return x
main :: IO ()
main = runScript $ do
contents <- replaceLeftT "Could not read file" $
scriptIO $ BSL.readFile "somefile"
scriptIO $ putStrLn "Won't get here"
It feels clunky to me, like I'm missing a fundamental concept. Probably
because I derived this function mostly by trial and error...
Any suggestions for a way to do this using existing Control.Error
primitives, or monad primitives in general?
No comments:
Post a Comment