使用`throwError'没有MyClass的实例

时间:2015-04-26 01:40:25

标签: haskell types monads monad-transformers

我有打字问题。我开始用this article学习Monad变形金刚。然后我很少改变它们的例子。现在,我的代码是:

data PwdError = PwdError String

type PwdErrorMonad = ErrorT PwdError IO

isValid :: String -> ErrorT String PwdErrorMonad Bool
isValid s
    | length s < 5 = throwError "password is short!"
    | otherwise = return True

现在,我有错误:

No instance for (Error PwdError) arising from a use of `throwError'
In the expression: throwError "password is short!"
In an equation for `isValid':
    isValid s
      | length s < 5 = throwError "password is too short!"
      | otherwise = return True

你能帮我编译这个程序吗?

1 个答案:

答案 0 :(得分:1)

您需要PwdError the Error typeclass的实例。

以下内容应该足够,但我还没有尝试编译它:

instance Error PwdError where
  strMsg = PwdError