3 ms·
The usual implementation of the Either/Option monads already does this though: instance Monad Maybe where return x = Just x Nothing >>=
by steveklabnik 11y ago
The usual implementation of the Either/Option monads already does this though:
instance Monad Maybe where
return x = Just x
Nothing >>= f = Nothing
Just x >>= f = f x
fail _ = Nothing
and
instance (Error e) => Monad (Either e) where
return x = Right x
Right x >>= f = f x
Left err >>= f = Left err
fail msg = Left (strMsg msg)
The end result is still a value of that type. You get the short-circuting behavior as soon as you hit Nothing/Left.
- pcwalton 11y agoIt only short-circuits to the end of the do block. But once you leave the do block the error isn't propagated anymore.
- steveklabnik 11y agoRight. Maybe this is because I tend to write small functions, but the amount of things that end in `Ok(())` especially with IO still makes me think it would be useful.