[Haskell-cafe] Monadic function fails as Nothing in Maybe context but as Exception in Either context.

Bryan Richter b at chreekat.net
Mon Nov 30 22:40:51 UTC 2015


On Mon, Nov 30, 2015 at 02:25:49PM -0800, Jeffrey Brown wrote:
> I've written a monadic function which in a Maybe context produces a Nothing
> when it fails (as intended), but in an Either context produces an Exception
> rather than a Left.

The Maybe instance for Monad overrides fail to be Nothing, but Either
instance does not. The default definition for fail is error.

The Except type has the behavior you are looking for, I believe — but
you may be better off avoiding the use of fail to begin with. :)

Except/ExceptT:
http://haddock.stackage.org/lts-3.16/transformers-0.4.2.0/Control-Monad-Trans-Except.html

Also recommended for all your error-ful needs:
http://haddock.stackage.org/lts-3.16/errors-2.0.1/Control-Error.html

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20151130/865a0d3f/attachment.sig>


More information about the Haskell-Cafe mailing list