[Haskell-cafe] Why does the transformers/mtl 'Error' class exist?
John Millikin
jmillikin at gmail.com
Thu Apr 15 22:18:53 EDT 2010
Both the transformers[1] and mtl[2] define a class named 'Error', for
use with MonadError and ErrorT. This class is required for the
instance of Monad (and therefore MonadTrans, MonadIO, etc). However, I
can't figure out why this class exists. Its presence means that
instead of something like:
-------------------------------------
data NameError = ErrorFoo | ErrorBar
validateName :: Monad m => Text -> m (Either Error Text)
validateName x = runErrorT $ do
when (some condition) $ throwError ErrorFoo
when (other condition) $ throwError ErrorBar
return x
-------------------------------------
I have to define this, which is more verbose, no more useful, and adds
a "fake" class to the Haddock docs (with a warning not to use it):
-------------------------------------
data Error = ErrorFoo | ErrorBar
instance Error NameError where
strMsg = error
-- validateName ...
-------------------------------------
Is there any good reason why the 'Error' class can't just be removed?
[1] http://hackage.haskell.org/packages/archive/transformers/0.2.0.0/doc/html/Control-Monad-Trans-Error.html
[2] http://hackage.haskell.org/packages/archive/mtl/1.1.0.2/doc/html/Control-Monad-Error-Class.html
More information about the Haskell-Cafe
mailing list