推导(Show,Eq)与数据类型的功能

时间:2018-11-24 11:09:44

标签: haskell

我有一些数据类型,作为参数我有一个函数

data Event =
   BasketCreated{basket:: Basket}
  | ProductAdded{ basket::Basket, addProductInBasket :: Basket -> String -> Int -> Basket}
  | DiscountAdded{ basket::Basket, addDiscountInBasket :: Basket  -> Int -> Basket }
  deriving (Show, Eq)

很遗憾,因为我的数据类型包含函数deriving (Show, Eq)未编译。

任何想法我都该如何deriving (Show)使用能够打印该数据类型的功能

2 个答案:

答案 0 :(得分:6)

正如注释中已经提到的,如果您的数据类型包含函数,则无法派生Show实例。最好的办法是自己实现实例。例如:

instance Show Event where
  show (BasketCreated x)   = "BasketCreated " ++ show x
  show (ProductAdded x _)  = "ProductAdded "  ++ show x
  show (DiscountAdded x _) = "DiscountAdded " ++ show x

答案 1 :(得分:3)

没有 public void uploadToSever(List<File> imgList) { AndroidNetworking.upload("your sever url") .addMultipartFileList("image[]", imgList) .addMultipartParameter("key", "value") .setPriority(Priority.HIGH) .build() .setUploadProgressListener(new UploadProgressListener() { @Override public void onProgress(long bytesUploaded, long totalBytes) { // do anything with progress } }) .getAsJSONObject(new JSONObjectRequestListener() { @Override public void onResponse(JSONObject response) { // do anything with response } @Override public void onError(ANError error) { // handle error } }); } 函数实例。无法成为Show实例的原因,请参阅:Show instance for functions