简体   繁体   中英

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

I have a problem with typing. I started to study Monad transformers with this article . Then I little changed them example. Now, my code is:

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

Now, I have the error:

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

Could you help me to compile this program?

You need to make PwdError an instance of the Error typeclass .

The following should be sufficient, though I haven't tried to compile it:

instance Error PwdError where
  strMsg = PwdError

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM