[Haskell-cafe] MonadThrow and Either instance

Viktor Dukhovni ietf-dane at dukhovni.org
Fri Feb 7 01:49:35 UTC 2020


On Thu, Feb 06, 2020 at 03:30:15PM +0000, PICCA Frederic-Emmanuel wrote:

> So I need a function with this signature
> 
> pathAbsDir :: Text -> Either String (Path Abs Dir)
> pathAbsDir t = do
>   d <- try $ parseAbsDir (unpack . uncomment $ t)
>   case d of
>     Right v -> Right v
>     Left e  -> Left (show e)
> 
> since I am using parseAbsDir wi this signature
> 
> parseAbsDir :: MonadThrow m => FilePath -> m (Path Abs Dir) 
> 
> I would like to know how to write the patAbsDir method

Try (no pun intended):

    import Control.Monad.Catch.Pure (runCatch)
    import Data.Text (Text, unpack)
    import Path (Path, Abs, Dir, parseAbsDir)

    pathAbsDir :: Text -> Either String (Path Abs Dir)
    pathAbsDir t = do
        let d = runCatch $ parseAbsDir (unpack t)
        case d of
            Right v -> Right v
            Left e  -> Left $ show e

-- 
    Viktor.


More information about the Haskell-Cafe mailing list