没有因使用“throwError”而产生的 MyClass 实例

No instance for MyClass arising from a use of `throwError'

我打字有问题。我开始用 this article 研究 Monad transformer。然后我几乎没有改变他们的例子。现在,我的代码是:

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

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

您需要使 PwdError 成为 the Error typeclass 的实例。

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

instance Error PwdError where
  strMsg = PwdError