tags:

views:

104

answers:

1

I have a function that returns type ErrorT String IO (). While the function works, liftIO's litter every line that does IO. It makes for a mess. Is there any way to get around this and still have the ability to abort on error?

+2  A: 

I assume this is the context of the question, so I'll repost the comment I left over there in case you didn't notice it:

If you use a few particular functions a lot, you could write a wrapper around them, e.g. liftedPutStr = liftIO . putStr. You could even import the originals qualified and make your lifted version use the same name, if you wanted. Also, a group of IO actions that won't raise errors can be pulled out into a single, separate function that can then be liftIOd just once. Does that help?

In case you're not familiar with qualified imports, here's putStr again as an example:

import Prelude hiding (putStr)
import qualified Prelude as P
import Control.Monad.Trans

putStr x = liftIO $ P.putStr x

That should let you use the altered putStr in a transformed IO the same way you'd normally use the real putStr in plain IO.

camccann
Thanks. I understand I can define additional functions that hide the liftIO but I suppose there isn't a visually cleaner way to do this -- one way or another I must make the types fit.
me2
Nothing else that I'm aware of. For keeping the important code visually cleaner, my suggestion would be to write a bunch of lifting wrappers in a separate module and import that. Still a bunch of junk code, but at least you don't have to look at it most of the time! I'm actually surprised a library doesn't already exist to lift the standard library `IO` actions into the `MonadIO` type class, but I just skimmed hackage and didn't see anything that seemed to do that.
camccann