开发者

Could not deduce type

开发者 https://www.devze.com 2023-04-06 03:02 出处:网络
I am attempting to pattern match on a type in a case statement such as the following: result <- action

I am attempting to pattern match on a type in a case statement such as the following:

result <- action
case result of
  Success _ -> do something
  Failure e -> case e of
                 MyException myField -> do take another action
                 _ -> ...

The compiler can't deduce e ~ MyException which I understand. My 开发者_运维技巧question is what other information do I need to supply to the compiler to be able to match my exception type. In this particular case I know that if there is a Failure the returned type will be MyException.

EDIT:

The type of result (From the Aws package) is: (Transaction r a, ConfigurationFetch (Info r)) => Configuration -> r -> IO (Response (ResponseMetadata a) a)

a is from Data.Attempt which is either a Success or Failure.


Assuming you're using extensible exceptions (which is the default in recent ghc's), your result is probably something like

data Result = Success MySuccess | Failure SomeException

You need to convert the SomeException to your own exception type. This is done with the function toException :: Exception e => SomeException -> Maybe e. Then you would handle this like:

Failure e -> case toException e of
               Just (MyException myField) -> do take another action
               _ -> ...

Of course this is assuming that I'm right about your Result type.

0

精彩评论

暂无评论...
验证码 换一张
取 消