我正在编写使用HTTP请求的函数集wor,并且需要创建一组用于处理失败的异常.这里是
data HStream ty => ErrorResponse ty = ErrorResponse (Response ty)
data HStream ty => HttpException ty = WrongURIException String | ConnException ConnError | RequestException (ErrorResponse ty)
instance HStream ty => Exception (HttpException ty)
Run Code Online (Sandbox Code Playgroud)
其中,WrongURIException对应于格式ConnException错误的uri,TCP堆栈中的错误,以及处理非2xx响应代码响应的RequestException.在instance Exception第3行声明之前,我应该派生出Typeable但是我在类型中迷失了.我该怎么办?
我建议不这样做.数据类型上下文在各方面都很糟糕.他们被弃用的原因是有原因的.如果你真的,真的,想要它们,请使用GADT.
如果你不使用上下文,这是微不足道的
{-# LANGUAGE DeriveDataTypeable #-}
import Data.Typeable
import Data.Data
data ErrorResponse ty = ErrorResponse (Response ty)
deriving(Data, Typeable, Show)
data HttpResponse ty = WrongURIException String
| ConnException ConnError
| RequestException (ErrorResponse ty)
deriving(Data, Typeable, Show)
instance (Typeable ty, Show ty) => Exception (HttpException ty)
Run Code Online (Sandbox Code Playgroud)
特别是GHC 7.8的你不能让你自己的实例为Typeable和Data,所以推导是正确的做法.