Hacker News new | ask | show | jobs
by steveklabnik 3807 days 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.
1 comments

It only short-circuits to the end of the do block. But once you leave the do block the error isn't propagated anymore.
Right. 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.