Should I avoid using Monad fail?

后端 未结 3 1082
-上瘾入骨i
-上瘾入骨i 2021-02-05 00:53

I\'m fairly new to Haskell and have been slowly getting the idea that there\'s something wrong with the existence of Monad fail. Real World Haskell warns against its use (\"Once

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-05 01:52

    I try to avoid Monad fail wherever possible, and there are a whole variety of ways to capture fail depending on your circumstance. Edward Yang has written a good overview on his blog in the article titled 8 ways to report errors in Haskell revisited.

    In summary, the different ways to report errors that he identifies are:

    1. Use error
    2. Use Maybe a
    3. Use Either String a
    4. Use Monad and fail to generalize 1-3
    5. Use MonadError and a custom error type
    6. Use throw in the IO monad
    7. Use ioError and catch
    8. Go nuts with monad transformers
    9. Checked exceptions
    10. Failure

    Of these, I would be tempted to use option 3, with Either e b if I know how to handle the error, but need a little more context.

提交回复
热议问题