简体   繁体   English

无法推断类型

[英]Could not deduce type

I am attempting to pattern match on a type in a case statement such as the following: 我正在尝试对case语句中的类型进行模式匹配,例如:

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. 编译器无法推断出我理解的e〜MyException。 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. 在这种特殊情况下,我知道如果发生故障,则返回的类型将为MyException。

EDIT: 编辑:

The type of result (From the Aws package) is: (Transaction ra, ConfigurationFetch (Info r)) => Configuration -> r -> IO (Response (ResponseMetadata a) a) 结果的类型(来自Aws包)是:(事务ra,ConfigurationFetch(信息r))=>配置-> r-> IO(响应(ResponseMetadata a)a)

a is from Data.Attempt which is either a Success or Failure. a来自Data.Attempt,它是成功还是失败。

Assuming you're using extensible exceptions (which is the default in recent ghc's), your result is probably something like 假设您使用的是可扩展的异常(这是最近的ghc的默认设置),则结果可能类似于

data Result = Success MySuccess | Failure SomeException

You need to convert the SomeException to your own exception type. 您需要将SomeException转换为您自己的异常类型。 This is done with the function toException :: Exception e => SomeException -> Maybe e . 这是通过功能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. 当然,这是假设我对您的Result类型是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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