undo_all
undo_all

Reputation: 139

How to print the type (Either String (IO String))?

It's a very, very long story, and I won't bore you with it, but basically, I managed to get myself in a situation in which I need to be able to print the type Either String (IO String). Any help?

Upvotes: 3

Views: 1411

Answers (2)

jamshidh
jamshidh

Reputation: 12070

The solution is not a one liner....

The IO monad isn't an instance of Show, so you can't just use print. In fact, the value in the IO monad has to be obtained first.

You can view the value of x::Either String (IO String) by putting this in your main....

case x of
    Left s -> putStrLn ("Left " ++ show s)
    Right getVal -> do
             s <- getVal
             putStrLn ("Right (IO " ++ show s ++ ")")

and it should resolve and print the value.


Edit-

I've been proven wrong by @luqui, :), which is cool, because I learned something....

Of course now I need to go one step further and put out a one-liner with the appropriate Left and Right designation. :)

either (print . ("Left " ++)) ((print =<<) . fmap ("Right IO " ++))

Upvotes: 7

luqui
luqui

Reputation: 60463

The solution is a one liner....

either print (print =<<)

If you want to demarcate whether it was Left or Right it's a bit more involved, see @jamsihdh's answer.

Note that this cannot be made a Show instance, since nothing can be purely observed about values of type IO a.

Upvotes: 14

Related Questions