Say I have a function
function1 :: (MonadError String m, MonadIO m) => m Int
function2 :: (MonadError Int m, MonadIO m) => m Int
and a function to transform Int
into a String
renderException :: Int -> String
Is there a way to implement function3, which is reusing all 3 functions?
function3 :: (MonadError String m, MonadIO m) => m Int
-- add the results from function1, function2 and
-- transform function2 error into a String
It turns out that I can use runExceptT
:
-- runExceptT :: ExceptT e m a -> m (Either e a)
function3 :: (MonadError String m, MonadIO m) => m Int
function3 =
do ei <- runExceptT function1
a <- either (throwError . show) pure ei
b <- function2
return (a + b)
So there's a way out of the MonadError e
"trap". On the other hand I don't know how to encapsulate this runExcept / either throwError
pattern.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With