How can we interact with 2 MonadError constraints having 2 different types of errors?

Say I have a function

function1 :: (MonadError String m, MonadIO m) => m Int

function2 :: (MonadError Int m, MonadIO m) => m Int

and conversion function InttoString

renderException :: Int -> String

Is there a way to implement function 3 that reuses all 3 functions?

function3 :: (MonadError String m, MonadIO m) => m Int
-- add the results from function1, function2 and 
-- transform function2 error into a String
+4
source share
1 answer

Turns out I can use runExceptT:

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 is a way out of the trap MonadError e. On the other hand, I do not know how to encapsulate this template runExcept / either throwError.

+1
source

Source: https://habr.com/ru/post/1688140/


All Articles