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
CodePudding user response:
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