Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why doesn't fmap either error id over an Either work?

why does the following code not cause an error? Instead, it exits immediately.

someAction :: IO (Either String ())
someAction = return $ Left "Error"

main :: IO ()
main = either error id <$> someAction

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When an action of type IO () is executed, its return value (the unit) is not evaluated. error, unlike ioError, is presenting as a pure function of type String -> a. To simplify your example:

main :: IO ()
main = pure $ error "error"

Running main as an IO action performs all side-effects (of which there are none) but doesn’t do anything with the return value, so it is not evaluated.

This, however, will error, as print evaluates its argument (even ()):

main >>= print
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading