【问题标题】:No instance for MyClass arising from a use of `throwError'没有因使用“throwError”而产生的 MyClass 实例
【发布时间】:2015-04-26 01:40:25
【问题描述】:

我打字有问题。我开始使用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

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

【问题讨论】:

  • 您需要在代码中的某处添加instance Error PwdError where ...。查看Error 类的文档,了解您需要实现哪些功能。

标签: haskell types monads monad-transformers


【解决方案1】:

您需要将PwdError 设为the Error typeclass 的实例。

以下应该足够了,虽然我没有尝试编译它:

instance Error PwdError where
  strMsg = PwdError

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多