的代码看起来是这样的:在Haskell中,如何自动派生这样的自定义类?
class MyAnd a where
myAnd :: (Show a) => a -> a -> String
x `myAnd` y = (show x) ++ " and " ++ (show y)
data TrafficLight = Red | Yellow | Green deriving(Show, MyAnd)
这里MyAnd
是具有功能myAnd
一个类型的类,我认为这是通用的,唯一的限制是a
已经到了Show
类的一个实例..
在TrafficLight
类型,它已经从Show
型类派生的。然而,当我编译代码时,编译器抱怨
Can't make a derived instance of ‘MyAnd TrafficLight’:
‘MyAnd’ is not a derivable class
In the data declaration for ‘TrafficLight’
Failed, modules loaded: none.
有没有人有关于此的想法?
真,编译器不知道什么方法都应该做的。但是,也许这种语言的一个有价值的补充将是一种告诉它的方式。相关:http://stackoverflow.com/a/3864801/524504 – masterxilo