繁体   English   中英

Haskell中自定义类型类的通用实例?

[英]Generic instance of custom type class in Haskell?

假设我有以下数据结构:

data Dezi = Dezi1 Int | Dezi2 String | Dezi3 [Dezi] deriving(Show)

class TestInterface a where
    testInt :: a -> Dezi

instance TestInterface Int where
    testInt 0 = Dezi1 0
    testInt _ = Dezi2 "Nie nula"

instance Dezi a =>  TestInterface [a] where 
    testInt xs = Dezi3 $ map (\x -> testInt x) xs

在最后一个语句中,我正在尝试为我的类型类创建通用实例我认为类型'a'是Int或String,但编译器不满意:

`Dezi' is applied to too many type arguments
In the instance declaration for `TestInterface [a]'

我是初学者,仍在学习过程中。

谢谢!

Dezi是一种数据类型,而不是类型类。 类型不是“ Dezi例子”。 相反,你可能会说些什么

instance TestInterface a => TestInterface [a] where
  testInt xs = Dezi3 $ map testInt xs

这倒像是“做的名单a S的一个实例TestInterface ,查找实例以a并使用它。”

暂无
暂无

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

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