【发布时间】:2015-05-17 14:49:36
【问题描述】:
代码如下所示:
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.
有人对此有想法吗?
【问题讨论】:
标签: haskell functional-programming